Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Accessing WebApplicationContext from TestContext

  1. #1
    Join Date
    Oct 2008
    Posts
    8

    Default Accessing WebApplicationContext from TestContext

    Hi

    I am using AbstractTransactionalJUnit4SpringContextTests to do some MVC integration testing and I need to access the WebApplicationContext.

    It appears that TestContext with contextConfiguration makes a GenericApplicationContext available but I can't see how to access or create a WebApplicationContext.

    This is currently a showstopper for me - any assistance greatly appreciated.

    Thanks ........Geoff

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    8.3.7.5. TestContext support classes -> 8.3.7.5.2. JUnit 4.4 support classes:
    AbstractTransactionalJUnit4SpringContextTests:

    Abstract transactional extension of AbstractJUnit4SpringContextTests that also adds some convenience functionality for JDBC access. Expects a javax.sql.DataSource bean and a PlatformTransactionManager bean to be defined in the ApplicationContext.

    When you extend AbstractTransactionalJUnit4SpringContextTests you will have access to the following protected instance variables:

    *applicationContext: inherited from the AbstractJUnit4SpringContextTests superclass. Use this to perform explicit bean lookups or to test the state of the context as a whole.
    ...

  3. #3
    Join Date
    Oct 2008
    Posts
    8

    Default

    Denis, this *applicationContext is the GenericApplicationContext - there doesn't appear to be away of getting the WebAppliactionContext from it.

    Cheers .......Geoff

  4. #4
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

    Do you need WebApplicationContext specific methods available in your tests or are the context failing due to mvc related stuff?

    In case of failure, split you context in multiple files and leave out the mvc related stuff in one of them and use this one for testing.

    Maybe this is not sufficient for what you are trying to do though...

    G

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

    Default

    Hi guys,

    It is correct that the Spring TestContext Framework does not support creation of a WebApplicationContext out-of-the-box; however, there are certainly hooks in place to achieve this goal with custom code. For example, you might find Dhruba Bandopadhyay's recent blog on Using MockServletContext and ContextLoader with Spring 2.5 TestContext framework useful.

    Regards,

    Sam

  6. #6
    Join Date
    Oct 2008
    Posts
    8

    Default

    Hi Goran / Sam

    Many thanks for your replies.

    Goran : I'm seeing both, a XmlViewResolver fails context and I'm trying to recover command object property validation errors using RequestContext which requires WebApplicationContext.

    Sam : I'm still pretty new to the TestContext - I've followed Dhruba's suggestions and this does provide a XmlWebApplicationContext but Dependancy Injection no longer works (which it does using the default loader) even with @TestExecutionListeners({DependencyInjectionTestEx ecutionListener.class})

    Any further pointers, much appreciated.

    Thanks ............Geoff

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

    Default

    Hi Geoff,

    Quote Originally Posted by GeoffM View Post
    Sam : I'm still pretty new to the TestContext - I've followed Dhruba's suggestions and this does provide a XmlWebApplicationContext but Dependancy Injection no longer works (which it does using the default loader) even with @TestExecutionListeners({DependencyInjectionTestEx ecutionListener.class})
    I've not actually tried to use Dhruba's code; however, try moving the call to
    context.refresh() below the call to AnnotationConfigUtils.registerAnnotationConfigProc essors((BeanDefinitionRegistry) context.getBeanFactory()) and see if that makes a difference.

    If that doesn't work, consider posting a comment to Dhruba's blog.

    Regards,

    Sam

  8. #8
    Join Date
    Oct 2008
    Posts
    8

    Default

    Hi Sam

    Moving refresh() triggered - Caused by: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext. Adding a second refresh() made no difference.

    Am I trying to do something unusual (needing access to WebApplicationContext ) for MVC integration testing ? I would have thought creating / accessing a similiar applicationContext ( contextLoader : DispatchServlet ) within a testContext would be key.

    Thanks for your reponses - I'll see if Dhruba has a solution.

    Geoff
    Last edited by GeoffM; Oct 27th, 2008 at 12:23 PM. Reason: typo

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

    Default

    Hi Geoff,

    Quote Originally Posted by GeoffM View Post
    Moving refresh() triggered - Caused by: java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext. Adding a second refresh() made no difference.
    OK, that might be due to differences in the XmlWebApplicationContext and GenericApplicationContext class hierarchies. Like I said, I haven't actually tried to use Dhruba's code.

    Quote Originally Posted by GeoffM View Post
    Am I trying to do something unusual (needing access to WebApplicationContext ) for MVC integration testing ?
    I don't think it's that unusual: it's just something we didn't add to Spring 2.5.x out-of-the-box, since people typically test their web components (e.g., controllers, servlets, filters, etc.) outside of the ApplicationContext in unit tests with mock objects for the ServletContext, HttpServletRequest, etc. However, if you'd like to see explicit support for loading a WebApplicationContext with the Spring TestContext Framework, you might consider creating a JIRA issue to suggest this for Spring 3.0.

    Regards,

    Sam

  10. #10
    Join Date
    Oct 2008
    Posts
    8

    Default

    Hi Sam

    I've followed your advice and raised SPR-5243. Hopefully a few others will add their support and vote for it's inclusion.

    Thanks for your assistance ............Geoff

Posting Permissions

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