I want to do a relatively simple test. The class being tested (simplified but you get the idea):
@Named
@RequestScoped
public class SomeController {
@Inject
@RequestParam("someId")
Long someId;
public SomeClass getSomeClass() {
return new SomeClass(someId);
}
}
And the test:
@RunWith(Arquillian.class)
public class SomeControllerTest {
@Inject
private SomeController controller;
@Deployment
public static Archive<?> createTestArchive() throws IOException {
// trimmed out
}
@Test
public void testNullGoalModelInjection() {
//placeholder test so there are no errors during build
// I am happy to use setter injection and do
controller.setSomeId(1);
// even better if I could get that injected in too
assertNotNull(controller.getSomeClass());
}
}
In trying, this, I get the exception:
java.lang.IllegalStateException: Attempted to inject an HttpServletRequest before it has been initialized.
Which makes sense.
What I am really wondering is:
Is there a way to test such a bean through Arquillian (or something else) without it involving the creation of jsf pages and then using jsfunit / warp / or some other mechanism to invoke a http request?
In other words, how can I invoke a http request through the test which will get this bean generated for me - but without it requiring jsf files and suchlike to be in place.
Thanks for any help / suggestions.
JBossAS7 use a Arquillian Protocol that invoke the tests over JMX so there is no HttpRequest available at all.
Try changing the Protocol to use when interacting with JBossAS7 use as described here: https://community.jboss.org/message/722871#722871
Now the tests are invoked over HTTP to a servlet, and your test should run.