Skip to end of metadata
Go to start of metadata

Before Arquillian negotiates the execution of the test, it enriches the test class by satisfying injection points specified declaratively using annotations.

Test case injection points

Injection points are recognized in the following two locations:

  1. Field (any visibility)
  2. @Test method argument

Here's an example of a field injection in a test class:

Here's an example of a method argument injection in a test method:

Note that the @Inject annotation is not required when injecting into a method argument.

Default enrichers

There are three injection-based enrichers provided by Arquillian out of the box:

  • @Resource - Java EE resource injections
  • @EJB - EJB session bean reference injections
  • @Inject, @Resource, @EJB, @PersistenceContext and @PersistenceUnit - CDI-supported injections

The first two enrichers use JNDI to lookup the instance to inject. The CDI injections are handled by treating the test class as a bean capable of receiving standard CDI injections (non-contextual injection). Since CDI requires containers to satisfy all Java EE injection points on a CDI bean, the @Resource, @EJB, @PersistenceContext and @PersistenceUnit injections are supported transitively by the CDI enricher. In fact, because of CDI's tight integration with the container, @EJB injections in the test class are satisfied according to specification when the CDI enricher is available.

Resource injection

The @Resource annotation gives you access to any object which is available via JNDI. It follows the standard rules for @Resource (as defined in the Section 2.3 of the Common Annotations for the Java Platform specification).

EJB injection (without CDI support)

The @EJB annotation performs a JNDI lookup for the EJB session bean reference using the following equation in the specified order:

If no matching beans were found in those locations the injection will fail.

At the moment, the lookup for an EJB session reference relies on some common naming convention of EJB beans. In the future the lookup will rely on the standard JNDI naming conventions established in Java EE 6.

CDI injection

In order for CDI injections to work, the test archive defined with ShrinkWrap must be a bean archive. That means adding beans.xml to the META-INF directory. Here's a @Deployment method that shows one way to add a beans.xml to the archive:

In an application that takes full advantage of CDI, you can likely get by only using injections defined with the @Inject annotation. Regardless, the other two types of injection come in handy from time-to-time.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jan 21, 2013

    In the example of @Test method argument, there is something wrong. The content is


    public class testGreeting(Greeter greeter) {---}

    Look, the method is declared as

    public class testGreeting(...) {---}

    It must not be a class.

    1. Apr 22, 2013

      Thanks for comment, it is fixed now.

  2. Jul 29, 2013

    Maybe you should user :

    instead of

    in the CDI Injection sample code.

  3. Jun 18, 2014

    Though it is indirectly mentioned in the text (fact that you need a beans.xml in your archive), this will only work when your deployment is testable. Meaning @Deployment(testable = false) won't work.

  4. Dec 03, 2015

    When I add the CDI injection in my class file, eclipse sends an error The method create(String) is undefined for the type Paths.

    Any tips?