Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arquillian on WAS remote container and @PersistenceContext

For our integration tests I wanted to use Arquillian and followed all the steps in the 'getting started tutorial'. Since I have an existing application things are not so trivial to set up however I managed to fix most of the problems. Now I get a NPE exception which indicates that CDI is not working. According to the WAS 8.5 remote container documentation I realized that @PersistenceContextis not supported. This is unfortunate since then I probably can't use Arquillian or is there any other possibility?

The simplified code structure:

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;
}

So is there a possibility to test my application with Arquillian and the WAS remote container?

Update: Here is the stacktrace:

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)

Update:

Since I can't test with an easy JarArchive I use the following to create an shrinkwrap Enterprise like this:

@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;
}
like image 629
Lonzak Avatar asked Jan 09 '17 17:01

Lonzak


1 Answers

Few clues:

  1. Make sure you can successfully run the simplest test with CDI (so please remove a @PersistenceContext) and create the smallest possible deployment - so please prepare a single CDI bean without any dependencies.
  2. Be absolutely sure that the deployed archive is CDI-enabled. It has to contain beans.xml file.

If you ask me, I'd bet that the archive for deployment is not correctly prepared. This is not your fault, it always has been a complex, time-consuming task.

Please, just for the test purposes, change the archive type to JavaArchive and try below JUnit sample:

@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...
}

Later, if the above will work, proceed further with @PersistenceContext and check if arquillian-was-remote-8-container-adapter supports it now (because the documentation you mentioned is from 2014).
If I were you, I would also try to ask the Arquillian's community.

Hope it helps.

like image 82
G. Demecki Avatar answered Jan 03 '23 12:01

G. Demecki