문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top