Results 1 to 10 of 14

Thread: Accessing WebApplicationContext from TestContext

Hybrid View

  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

Posting Permissions

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