TestNG - 使用任何 @Before* 注释时注入失败,但无需使用任何 @Before* 注释即可工作
我想在我的 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
,@BeforeTest
,@BeforeMethod< /code>),测试用例失败,因为所有注入的资源均为空(utx和em以及我想测试的其他一些类)。
所以,我问自己和你们:哪里出了问题?
亲切的问候, 塞巴斯蒂安
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Before* 方法似乎被调用了两次。另请参阅 https://issues.jboss.org/browse/ARQ-104?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12577331#comment-12577331
检查带注释的方法中是否有任何注入的资源为空应该可以解决问题。现在一切正常。
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.