集成测试CDI 1.0和Spring 3.1中的作用域bean

在这篇博客文章中,我描述了如何在Spring和CDI中使用作用域bean进行集成测试。 一切都用小代码示例进行说明。 使用范围进行集成测试并不是特别容易。 想象一下存在于会话范围内的bean,例如UserCredentials 。 在集成测试中,通常没有HttpRequest或HttpSession可以使用(至少在不进行包含用户界面的测试时)。 因此,您需要一些基础架构来进行集成测试。 使用这两种技术,使此基础结构正常运行有点令人困惑。 获取您自己的照片。

如果您不熟悉CDI和Spring中的范围和上下文,请查看基础知识并获得有关不同范围的概述。

在Spring中集成测试作用域的bean

在Spring 3.1中,没有针对作用域会话或请求Bean的集成测试支持(请参阅此处 )。 它计划在Spring版本3.2中使用。 但是, 此链接说明了适用于我的解决方案。

首先,您需要为测试开发一个SessionScope。 目的是模拟HttpRequest和HttpSession。

package com.mycompany.springapp.scope;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.SessionScope;

public class SetupSession extends SessionScope implements InitializingBean {

 public void afterPropertiesSet() throws Exception {
  MockHttpServletRequest request = new MockHttpServletRequest();
  MockHttpSession session = new MockHttpSession();
  request.setSession(session);
  RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(
    request));
 }

}

要在您的test-beans.xml中将该类注册为会话范围管理对象,请执行以下操作:

注意,我在context:component-scan标记之后注册了作用域

最后,我编写了我的测试课:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert;

@ContextConfiguration("/test-beans.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class MyScopeBeanTest {
 
 @Autowired
 private MyScopeBean myScopeBean;
 
 @Test
 public void testBeanScopes() {
  Assert.isTrue(myScopeBean.getMyCustomScopedService().getName().equals("Test"));
  Assert.isTrue(myScopeBean.getMySessionScopedService().getName().equals("Test"));
 }

}

注意,我已经在作用域bean上调用了方法getName() 。 这对于确保范围确定有效。 客户端代理可能会在注入点被注入,但是如果您对代理进行调用,则它不会分别引用作用域对象和协作对象。

使用CDI集成测试作用域的bean

我用于集成测试CDI的工具是Arquillian 。 还有其他选择。 如果仅使用CDI类进行测试,则可以“本地”使用Weld。 但是,如果您也有EJB,那还不够。 Arquillian带有相当数量的传递依赖项。 让我们来看看如何使事情发展。

注意:没有Maven,您会迷失在这里的沙漠中,因此,我鼓励您使用它! 我已经为Helios尝试了m2eclipse,但对我来说却不起作用,我使用Maven 3返回了旧的命令行。

pom.xml文件的更改

这些示例假定您有一个Java EE项目正在运行,您还可以在此处查看如何设置新的Java EE 6项目。 要集成Arquillian,请对pom.xml文件进行以下更改:

在属性部分:

1.0.0.Alpha5

添加此存储库:

repository.jboss.org
  http://repository.jboss.org/nexus/content/groups/public
  default
  
     true
     never
     warn
  
  
     false
     always
     warn

这是官方的JBoss Maven存储库,所有Arquillian发行版都在其中。

将以下依赖项添加到pom.xml

junit 
   junit 
   4.8.1 
   test 
 


   org.jboss.arquillian
   arquillian-junit
   ${arquillian.version}
   test



   org.jboss.arquillian.container
   arquillian-glassfish-remote-3.1
   ${arquillian.version}
   test



   javax.enterprise
   cdi-api
   1.0-SP4
   test

第一个依赖项是您的JUnit框架来编写集成测试。 第二个依赖项将Arquillian与JUnit集成在一起。 第三个依赖项集成了您的部署容器。 对我来说,这是我的Glassfish安装。 最后一个依赖项是CDI API,该CDI API需要可用于CDI测试。

请注意,在第17行中,我将我的Glassfish 3.1安装用作部署容器,而Arquillian使用远程调用来执行测试。 您需要在此处配置自己的部署环境。 有关正确的artifactId值,请参见JBoss Maven Repo 。 使用Arquillian,您的目标环境也可以是嵌入式容器,例如JBoss Embedded AS,GlassFish Embedded或Weld SE。 在那种情况下,您不需要单独的容器安装和远程调用,它们都在本地运行(“内存中”)。

在为目标环境添加依赖项之后,您可以执行mvn eclipse:eclipse。

使用Arquillian和JUnit编写和执行测试

最后,我编写了我的第一个Arquillian集成测试类:

import javax.inject.Inject;

import junit.framework.Assert;

import org.jboss.arquillian.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.mycompany.jeeapp.scope.MyApplicationService;
import com.mycompany.jeeapp.scope.MyConversationService;
import com.mycompany.jeeapp.scope.MyDefaultService;
import com.mycompany.jeeapp.scope.MyRequestService;
import com.mycompany.jeeapp.scope.MyScopeBean;
import com.mycompany.jeeapp.scope.MySessionService;
import com.mycompany.jeeapp.scope.MySingletonService;
import com.mycompany.jeeapp.scope.extension.MyCustomScopeService;

@RunWith(Arquillian.class)
public class MyArquillianJUnitTest {

 @Inject
 private MyScopeBean myScopeBean;

 @Deployment
 public static JavaArchive createTestArchive() {
  return ShrinkWrap
    .create(JavaArchive.class, "test.jar")
    .addClasses(MyScopeBean.class,MyApplicationService.class,
      MyConversationService.class, MyDefaultService.class,
      MyRequestService.class, MySessionService.class,
      MySingletonService.class, MyCustomScopeService.class)
    .addAsManifestResource(EmptyAsset.INSTANCE,
      ArchivePaths.create("beans.xml"));
 }

 @Test
 public void testScopedBeans() {
  Assert.assertTrue(myScopeBean.getApplicationService().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getApplicationServiceWithNew().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getCustomScopeService().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getDefaultService().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getRequestService().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getSessionService().getSomeName()
    .equals("myName"));
  Assert.assertTrue(myScopeBean.getSingletonService().getSomeName()
    .equals("myName"));
 }

}

结论

Spring目前不为作用域bean提供集成测试支持。 令人惊讶的是,Spring一直非常重视所有测试主题。 我在博客中描述了一种解决方法。 完成这项工作并不难。 计划对3.2 M1版本提供完全集成测试支持。

Arquillian启用了CDI范围的bean测试。 我在设置过程中遇到了一些问题(请参阅下面的最后一段),如果您使用新技术,我认为这很常见。 您必须将所有测试中的bean传递到归档文件(请参阅@Deployment方法)这一事实是我需要在大型项目中尝试的:这真的是一个好主意吗? 有时,大型应用程序与来自不同程序包的数十个bean连接在一起。 很难预测在集成测试中使用了哪些bean。

问题与解决方案

一些Arquillian设置带有很多依赖项,以致您不能使用标准的Eclipse启动配置。 生成的命令行参数超出了Windows命令行指令的长度限制。 因此,我已使用Ant脚本开始测试。 该脚本仅用于说明。 您必须构建自己的Ant脚本。 您可以按照以下方式获取类路径信息:在Eclipse中,转到“文件>导出>常规> Ant构建文件”以生成您的类路径信息。 获取此类路径信息,并将其放入Ant JUnit测试启动脚本中。 我已经在这里记录了完整的Ant脚本

当我启动此Ant脚本时,一切对我来说都很好。 如果您有任何问题要告诉我,可以查看测试结果文件和server.log进行分析。

Arquillian设置期间出现更多错误消息

WELD-001303范围类型javax.enterprise.context.ConversationScoped没有活动上下文

-> ConversationScope通过EE规范绑定到JSF。 因此,在Arquillian背负的正常HTTP请求期间,它们将不会处于活动状态。

POST http:// localhost:4848 / management / domain / applications / application返回的响应状态为403

-> 404/403错误可能是部署问题,请检查server.log的根本原因(我是没有将所有必需的类添加到test.jar

执行命令行时发生异常。

无法运行程序“ D:\ dev_home \ java-6-26 \ bin \ javaw.exe”(在目录“ D:\ dev_home \ repositories \ git \ jee-app-weld \ jee-app-weld”中):CreateProcess错误= 87,Falscher参数

->类路径超出Windows命令行操作允许的长度。 您需要使用Ant脚本或Maven来运行测试。

ValidationException:DeploymentScenario包含未在注册表中缓存任何已定义容器的目标

->看到这里

WELD-000072声明钝化作用域的托管bean必须具有钝化能力。 Bean:具有限定符[@Any @Default]的托管Bean [com.mycompany.jeeapp.scope.example.UserCredentials类]

->您需要在会话和会话范围的bean上实现Serializable。

DeploymentScenario包含不在注册表中缓存任何定义的Container的目标。 _默认_

->看到这里

java.net.ConnectException:连接被拒绝:connect

->您的远程Java EE服务器安装未运行,请启动它!

参考:来自我们JCG合作伙伴 Niklas的“在CDI 1.0和Spring 3.1中对作用域Bean进行集成测试”。


翻译自: https://www.javacodegeeks.com/2012/01/integration-testing-scoped-beans-in-cdi.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值