[
https://issues.apache.org/jira/browse/WICKET-5926?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14593397#comment-14593397
]
Felipe Campos de Almeida edited comment on WICKET-5926 at 6/19/15 1:17 PM:
---------------------------------------------------------------------------
Hi Martin,
Thank's for the answer.
No, I've tried this already.
I was using the WicketTester like your suggestion when I was doing tests with
JUnit and Spring only. But now I have a different scenario and I removed
everything that was implemented with Spring and now is pure Java EE 6, and my
testing classes are JUnit with Arquillian.
That's why I need to reuse the XTestWebApplication installed from the web.xml,
from the container, like an web application do when deployed in a container,
and it's already initiated, because Arquillian gives me it.
When I'm testing with Arquillian, it gives me my ServletContext and Filter, I
just reuse it and find my resources or anything that I want like a normal web
application.
If I don't do this, I don't have a ServletContext from the container, just a
new one that the container doesn't know my resources, because ServletContext is
a mock from BaseWicketTester.
These lines above is overriding my XTestWebApplication.
{code:title=BaseWicketTester.java|borderStyle=solid}
servletContext = new MockServletContext(application, null);
application.setWicketFilter(filter);
// Exception because an existing Application can't change the name.
application.setName("WicketTesterApplication-" + UUID.randomUUID());
application.setServletContext(servletContext);
application.initApplication();
{code}
That's why I added in the code:
{code:title=BaseWicketTester.java|borderStyle=solid}
if(initializeApplication)
{code}
was (Author: felipecalmeida):
Hi Martin,
Thank's for the answer.
No, I tried this already.
I was using the WicketTester like this when was doing tests with Spring. But
now I don't have anything with Spring and everything that is Java EE 6 and
testing with Arquillian.
I need to reuse the XTestWebApplication installed from the web.xml, from the
container, like an web application do when deployed in a container, and it's
already initiated.
If I don't do this, I don't have a ServletContext from the container, just a
new one that the container doesn't know my resources. The ServletContext is a
mock and I need it from the container.
When I'm testing with Arquillian, it gives me my ServletContext and Filter, I
just reuse it and find my resources or anything that I want like a normal web
application.
These lines above is overriding my XTestWebApplication.
{code:title=BaseWicketTester.java|borderStyle=solid}
servletContext = new MockServletContext(application, null);
application.setWicketFilter(filter);
// Exception because an existing Application can't change the name.
application.setName("WicketTesterApplication-" + UUID.randomUUID());
application.setServletContext(servletContext);
application.initApplication();
{code}
That's why I added in the code:
{code:title=BaseWicketTester.java|borderStyle=solid}
if(initializeApplication)
{code}
> Arquillian Support with Container ServletContext in
> BaseWicketTester/WicketTester.
> ----------------------------------------------------------------------------------
>
> Key: WICKET-5926
> URL: https://issues.apache.org/jira/browse/WICKET-5926
> Project: Wicket
> Issue Type: Improvement
> Components: wicket
> Affects Versions: 6.10.0, 6.11.0, 6.12.0, 6.13.0, 6.14.0, 6.15.0, 6.16.0,
> 7.0.0-M2, 7.0.0-M3, 6.17.0, 6.18.0, 7.0.0-M4, 7.0.0-M5, 6.19.0, 6.20.0
> Environment: Ubuntu 14.04, JDK 8/7/6
> Reporter: Felipe Campos de Almeida
> Labels: automation, features, patch, test
>
> Hello all,
> I'm wondering if BaseWicketTester could support an ServletContext and
> WicketFilter provided by an web.xml configured and the container reading and
> installing my XTestWebApplication.java instead of using Mock everytime.
> I'm using Arquillian from http://arquillian.org.
> And this improvement is going to help to run my tests with success using
> webapp/WEB-INF/ configuration (like web.xml and so on).
> I've already done the code and I'll commit (after some tests to maintain the
> legacy) to my github: https://github.com/felipecalmeida/wicket
> In my initial tests, the wicket-core tests are passing.
> For this first commit, I kept the mock on Session, request and respond,
> because it's not a problem yet.
> I don't have yet a sample test to help to understand better this
> modification, because I've to create a sample project using Arquillian in my
> github. But I can take some screenshots later and upload here or in my
> github, if it helps.
> I'll post here the full code before commit:
> {code:title=BaseWicketTester.java|borderStyle=solid}
> /**
> * Creates a <code>WicketTester</code>. Constructor to keep the legacy
> code.
> *
> * @param application
> * a <code>WicketTester</code> <code>WebApplication</code>
> object
> * @param servletCtx
> * the servlet context used as backend
> */
> public BaseWicketTester(final WebApplication application, final
> ServletContext servletCtx)
> {
> // Keeping legacy code.
> this(application, servletCtx, true);
> }
>
> /**
> * Creates a <code>WicketTester</code>.
> *
> * @param application
> * a <code>WicketTester</code> <code>WebApplication</code>
> object
> * @param servletCtx
> * the servlet context used as backend
> * @param initializeApplication
> * if don't have an application, initialize it
> */
> public BaseWicketTester(final WebApplication application, final
> ServletContext servletCtx, boolean initializeApplication)
> {
> // Default is to initialize the application.
> if(initializeApplication)
> {
>
> if(servletCtx == null)
> {
> servletContext = new
> MockServletContext(application, null);
> }
> else
> {
> servletContext = servletCtx;
> }
> }
> else
> {
> // Uses the servletContext provided by the container.
> servletContext = application.getServletContext();
> }
>
> // Container that don't provide a WicketFilter.
> if(application.getWicketFilter() == null)
> {
> final FilterConfig filterConfig = new
> TestFilterConfig();
> WicketFilter filter = new WicketFilter()
> {
> @Override
> public FilterConfig getFilterConfig()
> {
> return filterConfig;
> }
> };
>
> application.setWicketFilter(filter);
> }
>
> httpSession = new MockHttpSession(servletContext);
> ThreadContext.detach();
> this.application = application;
> if(initializeApplication)
> {
> // FIXME some tests are leaking applications by not
> calling destroy on them or overriding
> // teardown() without calling super, for now we work
> around by making each name unique
> application.setName("WicketTesterApplication-" +
> UUID.randomUUID());
> }
>
> ThreadContext.setApplication(application);
> if(initializeApplication)
> {
> application.setServletContext(servletContext);
> // initialize the application
> application.initApplication();
> }
> // We don't expect any changes during testing. In addition we
> avoid creating
> // ModificationWatcher threads tests.
>
> application.getResourceSettings().setResourcePollFrequency(getResourcePollFrequency());
> // reconfigure application for the test environment
> application.setPageRendererProvider(new
> LastPageRecordingPageRendererProvider(
> application.getPageRendererProvider()));
> application.setRequestCycleProvider(new
> TestRequestCycleProvider(
> application.getRequestCycleProvider()));
> // set a feedback message filter that will not remove any
> messages
> originalFeedbackMessageCleanupFilter =
> application.getApplicationSettings()
> .getFeedbackMessageCleanupFilter();
>
> application.getApplicationSettings().setFeedbackMessageCleanupFilter(
> IFeedbackMessageFilter.NONE);
> IPageManagerProvider pageManagerProvider =
> newTestPageManagerProvider();
> if (pageManagerProvider != null)
> {
> application.setPageManagerProvider(pageManagerProvider);
> }
> // create a new session when the old one is invalidated
> application.getSessionStore().registerUnboundListener(new
> UnboundListener()
> {
> @Override
> public void sessionUnbound(String sessionId)
> {
> newSession();
> }
> });
> // prepare session
> setupNextRequestCycle();
> }
> {code}
> {code:title=WicketTester.java|borderStyle=solid}
> /**
> * Creates a <code>WicketTester</code> to help unit testing.
> Constructor to keep the legacy code.
> *
> * @param application
> * a <code>WicketTester</code> <code>WebApplication</code>
> object
> * @param servletCtx
> * the servlet context used as backend
> */
> public WicketTester(WebApplication application, ServletContext
> servletCtx)
> {
> super(application, servletCtx);
> }
>
> /**
> * Creates a <code>WicketTester</code> to help unit testing.
> *
> * @param application
> * a <code>WicketTester</code> <code>WebApplication</code>
> object
> * @param servletCtx
> * the servlet context used as backend
> * @param initializeApplication
> * if don't have an application, initialize it
> */
> public WicketTester(WebApplication application, ServletContext
> servletCtx, boolean initializeApplication)
> {
> super(application, servletCtx, initializeApplication);
> }
> {code}
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)