deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stephen More <stephen.m...@gmail.com>
Subject Example unit testing a servlet that uses injection
Date Tue, 28 Feb 2017 02:55:57 GMT
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 ?

Here is what I currently have:

@RunWith( CdiTestRunner.class )
@TestControl( projectStage =
org.apache.deltaspike.core.api.projectstage.ProjectStage.UnitTest.class )
public class MyServletTest
{
        private static org.slf4j.Logger log =
org.slf4j.LoggerFactory.getLogger( MyServletTest.class );

        @Inject
        private org.apache.deltaspike.core.spi.scope.window.WindowContext
windowContext;

        @Inject
        private org.apache.deltaspike.cdise.api.ContextControl
contextControl;

        @InjectMocks
        private MyServlet myServlet;

        @After
        public void teardown()
        {
                contextControl.stopContext(
javax.enterprise.context.ConversationScoped.class );
        }

        @Before
        public void init()
        {
                org.mockito.MockitoAnnotations.initMocks( this );

                contextControl.startContext(
javax.enterprise.context.ConversationScoped.class );
                windowContext.activateWindow( "testWindow" );
        }

        @Test
        public void testFinancialEstimateReport() 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 );
        }
}

But the @Inject is not happening inside of MyServlet...what is needed to
make this work ?

-Thanks

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message