deltaspike-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gerhard Petracek <gpetra...@apache.org>
Subject Re: Example unit testing a servlet that uses injection
Date Tue, 21 Mar 2017 09:20:00 GMT
great that my example with MyFacesContainerAdapter helped!

just as additional info:
if you would like to test a whole use-case with different pages (with one
test-method), you can also have a look at [1].

regards,
gerhard

[1]
https://github.com/CDIatWork/IdeaFork/blob/master/ideafork_ee6/src/test/java/at/irian/cdiatwork/ideafork/test/frontend/controller/IdeaForkBaseFlowTest.java



2017-03-21 3:17 GMT+01:00 Stephen More <stephen.more@gmail.com>:

> This marks the completion of this thread. My Sample test now works for a
> Named bean AND servlet using Faces Messages.
> Needed to leverage classes from package org.apache.myfaces.test.mock
>
> Thanks for your help Gerhard.
>
> On Mon, Mar 20, 2017 at 8:28 PM, Stephen More <stephen.more@gmail.com>
> wrote:
>
> > I am now utilizing org.apache.deltaspike.testcontrol.impl.jsf.
> MyFacesContainerAdapter
> > ( like os890 ) which allows me to add a facesMessage from within the
> Named
> > Bean and it is now testable. ( Hip Hip Horay ! )
> >
> > The question now, how can I get that same org.apache.deltaspike.jsf.
> > impl.listener.request.DeltaSpikeFacesContextWrapper to show up when
> > testing my servlet ?
> > ( Same project: https://github.com/mores/maven-examples/tree/master/
> > prime-deltaspike )
> >
> > On Sat, Mar 18, 2017 at 6:45 PM, Gerhard Petracek <gpetracek@apache.org>
> > wrote:
> >
> >> hi stephen,
> >>
> >> @BeanProvider:
> >> some people are using a dynamic lookup via #getContextualReference and
> >> some
> >> are using manual injection via #injectFields.
> >> you can find examples at [1].
> >>
> >> you mainly have the issue due to using the mock-framework (see the
> answer
> >> i've sent some minutes ago).
> >>
> >> regards,
> >> gerhard
> >>
> >> [1] http://deltaspike.apache.org/documentation/core.html#BeanProvider
> >>
> >>
> >>
> >> 2017-02-28 18:26 GMT+01:00 Stephen More <stephen.more@gmail.com>:
> >>
> >> > Where can I find an example of using BeanProvider to test a servlet
> >> using
> >> > injection ?
> >> >
> >> > On Tue, Feb 28, 2017 at 12:10 PM, Gerhard Petracek <
> >> gpetracek@apache.org>
> >> > wrote:
> >> >
> >> > > hi stephen,
> >> > >
> >> > > i guess people just used BeanProvider instead (if they needed to
> test
> >> > that
> >> > > part).
> >> > >
> >> > > regards,
> >> > > gerhard
> >> > >
> >> > >
> >> > >
> >> > > 2017-02-28 15:34 GMT+01:00 Stephen More <stephen.more@gmail.com>:
> >> > >
> >> > > > https://issues.apache.org/jira/browse/DELTASPIKE-1236  has been
> >> > created
> >> > > to
> >> > > > track this.
> >> > > >
> >> > > > On Tue, Feb 28, 2017 at 9:24 AM, Stephen More <
> >> stephen.more@gmail.com>
> >> > > > wrote:
> >> > > >
> >> > > > > Sure, I will file a jira ticket, now that I have confirmation
> that
> >> > this
> >> > > > is
> >> > > > > not supposed to work out of the box.
> >> > > > >
> >> > > > > I find it hard to believe I am the first person asking how
to
> >> test a
> >> > > > > servlet that uses injection.
> >> > > > >
> >> > > > > On Tue, Feb 28, 2017 at 8:27 AM, Gerhard Petracek <
> >> > > gpetracek@apache.org>
> >> > > > > wrote:
> >> > > > >
> >> > > > >> hi stephen,
> >> > > > >>
> >> > > > >> please file a jira-ticket for such an improvement.
> >> > > > >>
> >> > > > >> thx & regards,
> >> > > > >> gerhard
> >> > > > >>
> >> > > > >>
> >> > > > >>
> >> > > > >> 2017-02-28 13:38 GMT+01:00 Stephen More <
> stephen.more@gmail.com
> >> >:
> >> > > > >>
> >> > > > >> > 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.testcont
> >> rol.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.ap
> >> i.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.HttpServlet
> >> > > > >> Response.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