我对 Arquillian 测试和 CDI 注入有一个奇怪的问题:
这个 bean 在测试时工作得非常好:
@RequestScoped
public class Greeter {
@Inject
protected GreetingProvider greetingProvider;
public String doGreeting(String greet) {
System.out.println("Information: greetingProvider=" + greetingProvider);
return String.format("%s, %s!", greetingProvider.getGreetingWord(), greet);
}
}
唯一的例外是,在以下测试用例中,方法“testGreetingProviderInGreeterIsNotNull()”总是失败
@RunWith(Arquillian.class)
public class GreeterTest {
@Deployment
public static WebArchive createDeployment() {
WebArchive deployment = ShrinkWrap.create(WebArchive.class)
.addClass(Greeter.class)
.addClass(GreetingProvider.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") ;
return deployment;
}
@Inject
private Greeter greeter;
@Test
@InSequence(1)
public void testGreeterIsInjectedProperly() {
assertNotNull(greeter);
}
@Test
@InSequence(2)
public void getGreetingTest() {
String name = "John";
String expected = "Hello, " + name + "!";
String actual = greeter.doGreeting(name);
assertEquals(expected, actual);
}
@Test
@InSequence(3)
public void testGreetingProviderInGreeterIsNotNull() {
assertNotNull(greeter.greetingProvider);
}
}
我不明白为什么,如果字段 greetingProvider 为空,那么第二个测试方法也应该失败。
我的错误是什么?
GitHub 上提供了完整的源代码: https ://github.com/t3chris/ArquillianInjectionTests