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 <[email protected]>
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 <[email protected]>
> 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 <[email protected]>
>> 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 <[email protected]>
>> > 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
>> > >
>> > >
>> > >
>> >
>>
>
>

Reply via email to