TestNG Before Suite (performed out of container; no test class injections)

TestNG Before Test (performed out of container; no test class injections)

Deploy arquillian and @Deployment war

TestNG Before Class (performed out of container; no test class injections)

TestNG Before Method (performed out of container; no test class injections)

TestNG Before Suite (in container; no injections)

TestNG Before Test (in container; no injections)

TestNG Before Class (in container; no injections)

TestNG Before Method (in container; test class INJECTIONS available)

TestNG test method (in container; test class INJECTIONS available)

TestNG After Method (in container; test class INJECTIONS available)

TestNG After Class (in container; test class INJECTIONS available)

TestNG After Test (in container; test class INJECTIONS available)

TestNG After Suite (in container; test class INJECTIONS available)

TestNG After Method (performed out of container; no test class injections)

TestNG Before Method (performed out of container; no test class injections)

TestNG Before Suite (in container; no injections)

TestNG Before Test (in container; no injections)

TestNG Before Class (in container; no injections)

TestNG Before Method (in container; test class INJECTIONS available)

TestNG test method (in container; test class INJECTIONS available)

TestNG After Method (in container; test class INJECTIONS available)

TestNG After Class (in container; test class INJECTIONS available)

TestNG After Test (in container; test class INJECTIONS available)

TestNG After Suite (in container; test class INJECTIONS available)

TestNG After Method (performed out of container; no test class injections)

TestNG After Class (performed out of container; no test class injections)

Undeploy war

TestNG After Test (performed out of container; no test class injections)

TestNG After Suite (performed out of container; no test class injections)

Undeploy arquillian

Stop JBoss (?)

From what I observe, there are two overlapping TestNG lifecycles being performed. One lifecycle (in black text) is performed on the client side; the other lifecycle is performed in its entirety for each test method inside the container (red text). It appears that test class injections, resources, etc., are only available in the BeforeMethod, test methods and AfterMethod phases of the lifecycle (although the injections appear to “hang around” in the AfterClass, AfterTest and AfterSuite methods).

Interestingly, if a test class @Resource is coded, the BeforeMethod and following phases are NOT executed in container (ARQ-563), yet the test “passes”. Note: @Inject seems to work as documented.

Following is a log trace supporting the above summary (extraneous entires have been omitted).

Question: Is this how Arquillian and TestNG are designed to work together? It seems to follow the diagram provided in the Arquillian documentation, except the diagram does not really portray the overlapping lifecycles.

If this is the case, can one assume the client-side lifecycle is for arquillian's purposes (i.e. to start / stop services, deploy / undeploy wars)? If so, how can one code a BeforeMethod that is only invoked when running in container? Right now, the BeforeMethod executes both on the client and in container and if it depends on an injected object, it will fail when running on the client-side since the injections are not available there. (I am current coding: if (injectedObject != null) to distinguish between the two invocations.

It seems that it is not possible to avoid having 2 lifecycles with TestNG, or Arquillian developers at least don't know how to avoid it.

JUnit provides a concept of runners, where every runner has complete control over how a test is executed. Does anyone know if TestNG provides a similar concept?

The current solution for Arquillian in TestNG is only to provide hooks into the executed tests, so it's possible to add more methods to run, but not avoid running methods in the test (e.g. Before). The only thing that can be completely overriden is the actual test methods: See the TestNG docs.

I'm facing the same issue now when trying to create an Arquillian extensions for Junit5 - extensions can only add listeners, but I can't find a way to disable test normal execution and trigger execution in an Arquillian container.