我对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为null,则第二个测试方法也会失败。
我的错误是什么?
GitHub上提供了完整的源代码: https://github.com/t3chris/ArquillianInjectionTests
答案 0 :(得分:0)
我相信,但不是100%肯定,这是因为你错过了arquillian.xml
文件夹中的src/test/resources
,它应该强制默认协议为Servlet 3.默认情况下,在AS7 / EAP6中protocol是他们的JMX协议。当您使用他们的JMX协议时,RequestScope
对于交互不起作用。
您的arquillian.xml应如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<arquillian xmlns="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
<defaultProtocol type="Servlet 3.0" />
</arquillian>