Results 1 to 3 of 3

Thread: Testing using @ContextConfiguration and XmlWebApplicationContext and dwr

  1. #1

    Default Testing using @ContextConfiguration and XmlWebApplicationContext and dwr

    I am currently performing testing using the XmlWebApplicationContext() ->

    Code:
    XmlWebApplicationContext ctx = new XmlWebApplicationContext();
    				ctx.setConfigLocations(new String[]{"trident-resend-servlet.xml", "applicationContext-itest.xml"});
    				ctx.setServletContext(new MockServletContext(""));
    				ctx.refresh();
    However, I am trying to convert to annotations using :

    @ContextConfiguration(locations={"/trident-resend-servlet.xml", "/applicationContext-itest.xml"})


    Dwr requires that I use XmlWebApplicationContext() to perform the spring Dependency injections and i am getting the following error when using @ContextConfiguration...what would be a workaround?

    Code:
    12 May 2010 14:29:28,010 INFO  DefaultListableBeanFactory [] Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1342545: defining beans [tridentResendController,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,__dwrConfiguration,dwrController,__AjaxResendManager,resendManager,viewResolver,tridentResendService,endpointLister,resendRequester,resendActionListRequestMapper,resendCriteriaListRequestMapper,jmsTemplate,dataSource,jmsFactory,resendRequestMessageConverter]; root of factory hierarchy
    12 May 2010 14:29:28,010 ERROR TestContextManager [] Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@7981d5] to prepare test instance [testGetActiveEndpoints(com.bofa.gcib.gfx.trident.resend.service.DefaultTridentResendServiceITest)]
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
    	at org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests.runBare(AbstractJUnit38SpringContextTests.java:183)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:120)
    	at junit.framework.TestSuite.runTest(TestSuite.java:230)
    	at junit.framework.TestSuite.run(TestSuite.java:225)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dwrController': Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [org.directwebremoting.spring.DwrController@18c458] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@1343ed0: display name [org.springframework.context.support.GenericApplicationContext@1343ed0]; startup date [Wed May 12 14:29:27 CDT 2010]; root of context hierarchy
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:42)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:173)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
    	... 16 more
    Caused by: java.lang.IllegalStateException: WebApplicationObjectSupport instance [org.directwebremoting.spring.DwrController@18c458] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@1343ed0: display name [org.springframework.context.support.GenericApplicationContext@1343ed0]; startup date [Wed May 12 14:29:27 CDT 2010]; root of context hierarchy
    	at org.springframework.web.context.support.WebApplicationObjectSupport.getWebApplicationContext(WebApplicationObjectSupport.java:105)
    	at org.springframework.web.context.support.WebApplicationObjectSupport.getServletContext(WebApplicationObjectSupport.java:121)
    	at org.directwebremoting.spring.DwrController.afterPropertiesSet(DwrController.java:157)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    	... 32 more

  2. #2
    Join Date
    Jan 2006
    Location
    Zürich, Switzerland
    Posts
    423

    Default

    Quote Originally Posted by kamilski81 View Post
    Dwr requires that I use XmlWebApplicationContext() to perform the spring Dependency injections and i am getting the following error when using @ContextConfiguration...what would be a workaround?
    See the following JIRA issue and related forum threads (i.e., the links in the JIRA issue comments): https://jira.springsource.org/browse/SPR-5243

    Regards,

    Sam

  3. #3
    Join Date
    Sep 2010
    Posts
    7

    Post override dwrController with a controller that do nothing

    After analizing the stack trace I found that the error happened because org.directwebremoting.spring.DwrController is inheriting from org.springframework.web.context.support.WebApplica tionObjectSupport, a class that requires a web context, so the solution is to create two xxx-servlet.xml and a class to override the default DwrController behaviour when starting.

    Solution

    First, create somewhere in your project.

    DwrControllerWithTestSupport.java

    public class DwrControllerWithTestSupport extends DwrController {


    @Override
    public void afterPropertiesSet() throws Exception {
    // overriding and doing nothing
    }
    }

    Then in xxx-servlet-test.xml just override the dwrController declaration and this way you will get this second bean instantiated in testing environment.

    <bean id="dwrController" class="pe.gob.hndac.kardex.web.dwr.DwrControllerWi thTestSupport">
    </bean>


    Then, in you web tests superclass:

    @ContextConfiguration(locations = { "classpath:xxx-servlet.xml", "classpath:xxx-servlet-test.xml" },inheritLocations = true)
    @Ignore
    public class WebBaseContextSensitiveTest extends AbstractJUnit4SpringContextTests{

    }

    Note: I think this is a valid approach if you are declaring dwrController this way in xxx-servlet.xml

    <dwr:controller id="dwrController" debug="true" />

    or

    with the old fashion way:

    <bean id="dwrController" class="org.directwebremoting.spring.DwrController" >
    <property name="configurators">
    <list>
    <ref bean="dwrConfiguration" />
    </list>
    </property>
    <property name="debug" value="true" />
    </bean>

    <bean id="dwrConfiguration" class="org.directwebremoting.spring.SpringConfigur ator">
    <property name="creators">
    <map>
    ...
    </map>
    </property>
    </bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •