[ https://issues.apache.org/jira/browse/DELTASPIKE-1236?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15891479#comment-15891479 ]
John D. Ament commented on DELTASPIKE-1236: ------------------------------------------- [~mores] not sure if you saw my messages on the mailing list, so anyways I'll post a complete response. The problem is that you're mixing mockito and deltaspike. When I change your test class to be: {code:java} @RunWith(CdiTestRunner.class) public class MyServletTest { @Inject private org.apache.deltaspike.core.spi.scope.window.WindowContext windowContext; @Inject private org.apache.deltaspike.cdise.api.ContextControl contextControl; @Inject private MyServlet myServlet; @After public void teardown() { contextControl .stopContext(javax.enterprise.context.ConversationScoped.class); } @Before public void init() { contextControl .startContext(javax.enterprise.context.ConversationScoped.class); windowContext.activateWindow("testWindow"); } @Test public void testServlet() throws Exception { javax.servlet.http.HttpServletRequest request = org.mockito.Mockito .mock(javax.servlet.http.HttpServletRequest.class); javax.servlet.http.HttpServletResponse response = org.mockito.Mockito .mock(javax.servlet.http.HttpServletResponse.class); // org.mockito.Mockito.when( request.getServletPath() ).thenReturn( // "/this/path" ); // org.mockito.Mockito.when( request.getParameter( "ID" ) ).thenReturn( // "1234" ); // org.mockito.Mockito.when( request.getParameter( "format" ) // ).thenReturn( "PDF" ); myServlet.doGet(request, response); } } {code} It passes fine. But note, you can't mix the two. If you need to do mocking, you would use our MockManager so that its still handled as a CDI injection point - http://deltaspike.apache.org/documentation/test-control.html#MockFrameworks Using the mocks for request/response should be perfectly fine for your use case, but you can't expect @Inject and mockito to work together. > unit testing a servlet that uses injection > ------------------------------------------ > > Key: DELTASPIKE-1236 > URL: https://issues.apache.org/jira/browse/DELTASPIKE-1236 > Project: DeltaSpike > Issue Type: Improvement > Affects Versions: 1.7.2 > Environment: Tomcat > Reporter: Stephen More > > I have been using org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner > to test JSF backing beans and everything seems to be working well. > At this point I am struggling how to test a plain old servlet that uses > injection, are there any examples anywhere ? > The current - non working test code can be found here: > https://github.com/mores/maven-examples/tree/master/prime-deltaspike > Servlet works as expected when deployed - output is survey says: 3.96 > (https://github.com/mores/maven-examples/blob/master/prime-deltaspike/src/main/java/org/test/MyServlet.java) > But when trying to run the test, windowContext appears to be null. ( > https://github.com/mores/maven-examples/blob/master/prime-deltaspike/src/test/java/org/test/MyServletTest.java > ) -- This message was sent by Atlassian JIRA (v6.3.15#6346)