You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@deltaspike.apache.org by Stephen More <st...@gmail.com> on 2017/03/15 13:54:46 UTC
Re: Example unit testing a servlet that uses injection
I have evolved my use case, and I am looking for any more pointers in
getting the test working....
My named bean now sets a FacesMessage when over a limit (
https://github.com/mores/maven-examples/blob/master/prime-deltaspike/src/main/java/org/test/Product.java#L28
)
Servlet dumps any faces messages that exist (
https://github.com/mores/maven-examples/blob/master/prime-deltaspike/src/main/java/org/test/MyServlet.java#L53
)
Everything works as expected when deployed. What is needed to make the
unittest work ?
Do I need to mock FacesContextFactory or should that be handled by
CdiTestRunner ?
Are there any examples how this would be handled ? Is this where I need to
utilize the BeanProvider that was mentioned earlier ?
-Thanks for listening.
On Tue, Feb 28, 2017 at 8:55 AM, John D. Ament <jo...@gmail.com>
wrote:
> Stephen,
>
> I suspect the failure is because of the use of mockito. We provide some
> mocking support OOTB, would that help?
>
> http://deltaspike.apache.org/documentation/test-control.
> html#MockFrameworks
>
> John
>
> On Tue, Feb 28, 2017 at 7:38 AM Stephen More <st...@gmail.com>
> wrote:
>
> > 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
> > )
> >
> > Any and all help is always appreciated.
> >
> > On Mon, Feb 27, 2017 at 9:55 PM, Stephen More <st...@gmail.com>
> > wrote:
> >
> > > 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
> > >
> > >
> > >
> >
>
Re: Example unit testing a servlet that uses injection
Posted by Gerhard Petracek <gp...@apache.org>.
hi stephen,
mocks generated by a framework are supported with some limitations (see [1]
and [2]).
however, in several cases it's enough to use @Specializes and/or
@Alternative (and in some cases global-alternatives [3]) for your mocks.
provide those beans only in your test-classpath and you get a nice
replacement for generated mocks (see e.g. [4]).
regards,
gerhard
[1]
http://deltaspike.apache.org/documentation/test-control.html#OptionalIntegrations
[2]
https://github.com/os890/ee6-ds-demo/blob/master/src/test/java/org/os890/demo/ee6/test/MockedPageBeanTest.java
[3] https://deltaspike.apache.org/documentation/spi.html#GlobalAlternative
[4]
https://github.com/CDIatWork/IdeaFork/tree/master/ideafork_core/src/test/java/at/irian/cdiatwork/ideafork/test/core
2017-03-15 21:30 GMT+01:00 Stephen More <st...@gmail.com>:
> I think I start to run into issues when I try to utilize ContextMocker from
> http://illegalargumentexception.blogspot.com/2011/12/jsf-
> mocking-facescontext-for-unit-tests.html
>
> I have a feeling that ContextMocker is not compatible with
> CdiTestRunner.....
>
> On Wed, Mar 15, 2017 at 9:54 AM, Stephen More <st...@gmail.com>
> wrote:
>
> > I have evolved my use case, and I am looking for any more pointers in
> > getting the test working....
> >
> > My named bean now sets a FacesMessage when over a limit (
> > https://github.com/mores/maven-examples/blob/master/
> > prime-deltaspike/src/main/java/org/test/Product.java#L28 )
> >
> > Servlet dumps any faces messages that exist ( https://github.com/mores/
> > maven-examples/blob/master/prime-deltaspike/src/main/
> > java/org/test/MyServlet.java#L53 )
> >
> > Everything works as expected when deployed. What is needed to make the
> > unittest work ?
> >
> > Do I need to mock FacesContextFactory or should that be handled by
> > CdiTestRunner ?
> > Are there any examples how this would be handled ? Is this where I need
> to
> > utilize the BeanProvider that was mentioned earlier ?
> >
> > -Thanks for listening.
> >
> > On Tue, Feb 28, 2017 at 8:55 AM, John D. Ament <jo...@gmail.com>
> > wrote:
> >
> >> Stephen,
> >>
> >> I suspect the failure is because of the use of mockito. We provide some
> >> mocking support OOTB, would that help?
> >>
> >> http://deltaspike.apache.org/documentation/test-control.html
> >> #MockFrameworks
> >>
> >> John
> >>
> >> On Tue, Feb 28, 2017 at 7:38 AM Stephen More <st...@gmail.com>
> >> wrote:
> >>
> >> > 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
> >> > )
> >> >
> >> > Any and all help is always appreciated.
> >> >
> >> > On Mon, Feb 27, 2017 at 9:55 PM, Stephen More <stephen.more@gmail.com
> >
> >> > wrote:
> >> >
> >> > > 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
> >> > >
> >> > >
> >> > >
> >> >
> >>
> >
> >
>
Re: Example unit testing a servlet that uses injection
Posted by Stephen More <st...@gmail.com>.
I think I start to run into issues when I try to utilize ContextMocker from
http://illegalargumentexception.blogspot.com/2011/12/jsf-mocking-facescontext-for-unit-tests.html
I have a feeling that ContextMocker is not compatible with
CdiTestRunner.....
On Wed, Mar 15, 2017 at 9:54 AM, Stephen More <st...@gmail.com>
wrote:
> I have evolved my use case, and I am looking for any more pointers in
> getting the test working....
>
> My named bean now sets a FacesMessage when over a limit (
> https://github.com/mores/maven-examples/blob/master/
> prime-deltaspike/src/main/java/org/test/Product.java#L28 )
>
> Servlet dumps any faces messages that exist ( https://github.com/mores/
> maven-examples/blob/master/prime-deltaspike/src/main/
> java/org/test/MyServlet.java#L53 )
>
> Everything works as expected when deployed. What is needed to make the
> unittest work ?
>
> Do I need to mock FacesContextFactory or should that be handled by
> CdiTestRunner ?
> Are there any examples how this would be handled ? Is this where I need to
> utilize the BeanProvider that was mentioned earlier ?
>
> -Thanks for listening.
>
> On Tue, Feb 28, 2017 at 8:55 AM, John D. Ament <jo...@gmail.com>
> wrote:
>
>> Stephen,
>>
>> I suspect the failure is because of the use of mockito. We provide some
>> mocking support OOTB, would that help?
>>
>> http://deltaspike.apache.org/documentation/test-control.html
>> #MockFrameworks
>>
>> John
>>
>> On Tue, Feb 28, 2017 at 7:38 AM Stephen More <st...@gmail.com>
>> wrote:
>>
>> > 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
>> > )
>> >
>> > Any and all help is always appreciated.
>> >
>> > On Mon, Feb 27, 2017 at 9:55 PM, Stephen More <st...@gmail.com>
>> > wrote:
>> >
>> > > 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
>> > >
>> > >
>> > >
>> >
>>
>
>