I would like to use the @Inject annotation in my TestNG test case. The test is executed by Arquillian in a remote JBoss AS 6 instance. The test basically looks like this:
Test case
public class WorksheetControllerTest extends Arquillian {
@PersistenceContext
@Produces
@Default
EntityManager em;
@Inject
private UserTransaction utx;
@Deployment
public static WebArchive createTestArchive() {
return ShrinkWrap
.create( WebArchive.class, "test.war" )
.addClasses( SomeClass.class )
.addAsWebInfResource( new ByteArrayAsset( "<beans />".getBytes() ), ArchivePaths.create( "beans.xml" ) )
.addAsResource( "persistence-test.xml", "META-INF/persistence.xml");
}
//@BeforeClass
//@BeforeTest
@BeforeMethod
public void initTestData() throws Exception {
// ...
utx.begin();
em.persist( someEntity );
utx.commit();
}
@Test
public void testGetEmployeeFromTimesheet() throws Exception {
// ...
}
}
Working when …
If I manually call the initTestData() method in a single test method, I have properly injected resources to use.
Not-working when …
If I use any of the annotations given above (@BeforeClass, @BeforeTest, @BeforeMethod), the test case fails because all the injected resources are null (utx and em and some other classes I want to test).
So, I’m asking myself and you people: What is wrong there?
Kind regards,
Sebastian
The @Before* methods seem to be called twice. Also see https://issues.jboss.org/browse/ARQ-104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12577331#comment-12577331
Checking if any injected resources are null in the annotated method should do the trick. Everything works fine now.