Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 766B4200C29 for ; Tue, 28 Feb 2017 18:10:40 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 74D7E160B7C; Tue, 28 Feb 2017 17:10:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BE10B160B59 for ; Tue, 28 Feb 2017 18:10:39 +0100 (CET) Received: (qmail 19882 invoked by uid 500); 28 Feb 2017 17:10:39 -0000 Mailing-List: contact users-help@deltaspike.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@deltaspike.apache.org Delivered-To: mailing list users@deltaspike.apache.org Received: (qmail 19871 invoked by uid 99); 28 Feb 2017 17:10:38 -0000 Received: from mail-relay.apache.org (HELO mail-relay.apache.org) (140.211.11.15) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 28 Feb 2017 17:10:38 +0000 Received: from mail-it0-f44.google.com (mail-it0-f44.google.com [209.85.214.44]) by mail-relay.apache.org (ASF Mail Server at mail-relay.apache.org) with ESMTPSA id 9D5521A05A7 for ; Tue, 28 Feb 2017 17:10:38 +0000 (UTC) Received: by mail-it0-f44.google.com with SMTP id 203so13608386ith.0 for ; Tue, 28 Feb 2017 09:10:38 -0800 (PST) X-Gm-Message-State: AMke39nfn4ZTihFhjMsQhmXE1gOODwwRZgTZwFt4NDT+i6eg4WzPfwChYIkBxLPI5+BGg5J55d8/pCQjdCCLyw== X-Received: by 10.36.204.137 with SMTP id x131mr3843336itf.35.1488301838066; Tue, 28 Feb 2017 09:10:38 -0800 (PST) MIME-Version: 1.0 Received: by 10.79.36.78 with HTTP; Tue, 28 Feb 2017 09:10:17 -0800 (PST) In-Reply-To: References: From: Gerhard Petracek Date: Tue, 28 Feb 2017 18:10:17 +0100 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Example unit testing a servlet that uses injection To: users@deltaspike.apache.org Content-Type: multipart/alternative; boundary=94eb2c05bf746071df05499a46c9 archived-at: Tue, 28 Feb 2017 17:10:40 -0000 --94eb2c05bf746071df05499a46c9 Content-Type: text/plain; charset=UTF-8 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 : > 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 > 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 > > 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 : > >> > >> > 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 > > >> > 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.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 > >> > > > >> > > > >> > > > >> > > >> > > > > > --94eb2c05bf746071df05499a46c9--