我想在TestNG测试用例中使用@Inject注释.该测试由Arquillian在远程JBoss AS 6实例中执行.测试基本上如下:
测试用例
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 {
// ...
}
}
工作时……
如果我在单个测试方法中手动调用initTestData()方法,我已正确注入要使用的资源.
……时不工作
如果我使用上面给出的任何注释(@BeforeClass,@ BeyondTest,@ BeforeMethod),测试用例将失败,因为所有注入的资源都是null(utx和em以及我想测试的其他一些类).
所以,我问自己和你们的人:那里有什么问题?
亲切的问候,
塞巴斯蒂安
最佳答案 @ Before *方法似乎被调用了两次.另见
https://issues.jboss.org/browse/ARQ-104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12577331#comment-12577331
检查注释方法中是否有任何注入的资源为空应该可以解决问题.现在一切都很好.