在WAS远程容器上的Arquillian和@PersistenceContext

时间:2017-01-09 17:16:32

标签: java-ee integration-testing cdi jboss-arquillian

对于我们的集成测试,我想使用Arquillian,并按照“{3}}'中的所有步骤进行操作。”由于我有一个现有的应用程序,设置起来并不是那么简单,但我设法解决了大部分问题。现在我得到一个NPE异常,表明CDI无法正常工作。根据{{​​3}}文档,我意识到@PersistenceContext不受支持。这是不幸的,因为那时我可能不能使用Arquillian或者还有其他可能吗?

简化的代码结构:

class MyTest{ 

  @Deployment
  public static EnterpriseArchive createDeployment() {
  ...
  }

  @Inject
  public Service2Inject service;

  @Test
  public void testService() {
    //Assert.assertNotNull("Dependency injection failed!",service);
    String s = service.getSomeString("10");
    ...
  }
}


public class Service2Inject implements SomeInterface {

  @Inject
  private SomeOtherService serviceOther;

  @Override
  public String getSomeString(String id) {
    String testString = serviceOther.getSomeOtherStrings();
    ...
  }
}

public class SomeOtherService implements SomeInterface2 {

  @Inject
  private EntityManager entityManager;

  @Override
  public List<String> getSomeOtherStrings(String Id) {
    Query query = entityManager.createNamedQuery(...)
    ...
  }
}

public class EntityManagerProducer {
   @PersistenceContext(unitName="datasourcename")
   @Produces
   private EntityManager entityManager;
}

那么有可能用Arquillian和WAS远程容器测试我的应用程序吗?

更新:这是堆栈跟踪:

java.lang.NullPointerException
    at MyTest.testService(MyTest.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.jboss.arquillian.junit.Arquillian$8$1.invoke(Arquillian.java:379)
    at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
    at org.jboss.arquillian.container.test.impl.client.protocol.local.LocalContainerMethodExecutor.invoke(LocalContainerMethodExecutor.java:50)
    at org.jboss.arquillian.container.test.impl.execution.RemoteTestExecuter.execute(RemoteTestExecuter.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
    at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
    at org.jboss.arquillian.container.test.impl.execution.ClientTestExecuter.execute(ClientTestExecuter.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
    at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createContext(ContainerEventController.java:142)
    at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createTestContext(ContainerEventController.java:129)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:96)
    at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
    at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
    at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:136)
    at org.jboss.arquillian.junit.Arquillian$8.evaluate(Arquillian.java:372)
    at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:246)
    at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:431)
    at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:55)
    at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:260)
    at org.jboss.arquillian.junit.Arquillian$7.evaluate(Arquillian.java:330)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:205)
    at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:431)
    at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:55)
    at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:219)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:167)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

更新

由于我无法使用简单的JarArchive进行测试,因此我使用以下内容创建一个这样的shrinkwrap Enterprise:

@Deployment
public static EnterpriseArchive createDeployment() {
  EnterpriseArchive ear = ShrinkWrap.create(EnterpriseArchive.class, "application-ear.ear")
  .as(ZipImporter.class)
  .importFrom(new File("../application-ear/target/application-ear-1.0-SNAPSHOT.ear"))
  .as(EnterpriseArchive.class);

  //now add the testClass and any test util classes that are not in the archive
  JavaArchive testLibraryHelper = ShrinkWrap.create(JavaArchive.class)
  .addClass(testClass)
  .addPackage(MiscUtil.class.getPackage())
  .addPackage(some.other.UtilClass.class.getPackage())
  //now for CDI working in testLibraryHelper
  .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
  ear.addAsLibrary(testLibraryHelper);
  return ear;
}

1 个答案:

答案 0 :(得分:3)

几条线索:

  1. 确保您可以使用CDI成功运行最简单的测试(因此请删除@PersistenceContext)并创建尽可能小的部署 - 因此请准备一个没有任何依赖关系的CDI bean。
  2. 绝对确保部署的存档启用CDI 。它必须包含beans.xml文件。
  3. 如果你问我,我敢打赌,部署档案没有准备好。这不是你的错,它总是一项复杂,耗时的任务。

    请出于测试目的,将存档类型更改为JavaArchive和 尝试下面的JUnit示例:

    @RunWith(Arquillian.class)
    @Category(IntegrationTest.class)
    public class AbcIT {
    
      @Deployment
      public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addPackage("com.needed.package")
                .addClass("com.needed.class")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); // CRUCIAL to make CDI work
    
        System.out.println(jar.toString(true)); // Print to see the content
        return jar;
      }
    
      @Inject
      private WhateverService service;
    
      // your test goes here...
    }
    

    稍后,如果上述方法有效,请继续@PersistenceContext,然后检查arquillian-was-remote-8-container-adapter是否支持它(因为您提到的文档来自2014年)。
    如果我是你,我也会试着询问Arquillian的社区。

    希望它有所帮助。