7

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;
}
Lonzak
  • 9,334
  • 5
  • 57
  • 88
  • Are you trying to use WebSphere App Server traditional or WebSphere Liberty? – Scott Kurz Jan 09 '17 at 18:20
  • Can you share the stacktrace? I believe injection is not support directly to the test, but not within you app - this should work as long as your container has support for injecting it (which I believe is the case for WAS). – bartosz.majsak Jan 10 '17 at 15:38
  • @Scott Kurz: WebSphere App Server traditional – Lonzak Jan 10 '17 at 16:15
  • @bartosz.majsak I added the stacktrace. Basically when accessing the injected service a NPE is thrown since it is null. – Lonzak Jan 11 '17 at 17:28
  • The strange thing is that I don't get any other error message - seems to me that CDI isn't (silently) working at all... – Lonzak Jan 12 '17 at 09:02
  • 1
    That's madness. I don't envy you... If I were you, I would try to change the way you create the EnterpriseArchive and replace `.addAsLibrary()` with `.addAsModule()`. If it won't work, please try to switch to the `WebArchive` - because it seems to be supported by the arquillian remote WAS adapter. – G. Demecki Jan 25 '17 at 09:21

1 Answers1

3

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.

G. Demecki
  • 10,145
  • 3
  • 58
  • 58
  • The JavaArchive type is not supported by the WAS server – Lonzak Jan 25 '17 at 07:41
  • How this could be possible? Are you 100% sure? This would mean that an ejb jar cannot be deployed to a WebSphere - which sounds ridiculously. – G. Demecki Jan 25 '17 at 07:59
  • Yes that's the case. The error you get is: `org.jboss.arquillian.container.spi.client.container.DeploymentException: Unsupported archive type has been provided for deployment: org.jboss.shrinkwrap.impl.base.spec.JavaArchiveImpl`. Thats why I use an `EnterpriseArchive` file in my tests – Lonzak Jan 25 '17 at 08:29
  • That would be ridiculous. I encourage you to contact directly the Arquillian's community [here](http://arquillian.org/community/) or fill a bug report for the arquillian was remote adapter. – G. Demecki Jan 25 '17 at 08:43
  • 1
    I actually did that already and nobody proved me otherwise: https://developer.jboss.org/thread/273575 And furthermore I checked the source code of the implementation and as you can see for yourself there is only WebArchive and EnterpriseArchive (`deploy` method): https://github.com/arquillian/arquillian-container-was/blob/master/was-remote-8/src/main/java/org/jboss/arquillian/container/was/remote_8/WebSphereRemoteContainer.java – Lonzak Jan 25 '17 at 08:50
  • Ok I gave up. I invested one more day however wasn't successful and since we are switching to JBOSS this year I can't justify putting more time on it... – Lonzak Jan 27 '17 at 09:05