Results 1 to 3 of 3

Thread: ApplicationContext Error ....TestCase

  1. #1

    Question ApplicationContext....TestCase...

    I have the following code I am testing my controller with a Junit testcase if i run the testcase it runs with the following error

    "ApplicationObjectSupport instance [BasicController@13f210f] does not run in an ApplicationContext "

    But when I comment the following line
    Code:
    ApplicationContext context = this.getApplicationContext();
    inside BasicController file my testcase runs successfully...

    Please help what i am doing wrong I need that Context for checking the layout...

    How can i test this what changees need to be done inside testCase file..


    Please Help..

    Regards

    BuntyIndia






    BasicController

    Code:
    public class BasicController extends UrlFilenameViewController
    {
        protected ModelAndView handleRequestInternal(HttpServletRequest aRequest,
            HttpServletResponse aResponse)
        {
    
            ModelAndView mv = super.handleRequestInternal(aRequest, aResponse);
            mv.getModel().put("b", mv.getViewName() + ".jsp");
            
            ApplicationContext context = this.getApplicationContext();
          
            /**Do Some thing with the Context and return MV with layout**/
            
            return mv;
    
        }
    testBasicController
    Code:
    public class testBasicController extends TestCase {
        
    	public void testGettingIndexPage() throws Exception {
    
    	BasicController controller = new BasicController();
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
    
            request.setMethod("GET");
    	request.setContextPath("/POC");
    	request.setRequestURI("/index.htm");
            
            ModelAndView modelAndView = controller.handleRequest(request, response);
    
            assertEquals("Should get Index page", modelAndView.getModel().get("b") , "index.jsp");
        }
    }
    Last edited by buntyindia; Sep 24th, 2007 at 10:49 PM.

  2. #2
    Join Date
    Sep 2007
    Location
    Oceanside, CA
    Posts
    187

    Default

    In your unit test, you instantiate an instance of BasicController directly:

    Code:
      BasicController controller = new BasicController();
    Because the Spring container is not being used to create this object, the application context will not be available.

    If you need access to the application context, you could extend AbstractDependencyInjectionSpringContextTests and use the Spring container to inject an instance of BasicController into your test (making this more of an "integration test" than a "unit test"), or mock the ApplicationContext for the purposes of your unit test and call controller.setApplicationContext with the mock implementation.
    Mike Bingham

  3. #3

    Default

    Thanks for the help Mike.

    I created a Application context entry as follows in my testCase

    Code:
    private ApplicationContext ctx;
    ctx = new FileSystemXmlApplicationContext("/web/WEB-INF/applicationContext.xml");
    And also slightly changed the Bean Instantiation line

    Code:
    BasicController controller = (BasicController)ctx.getBean("basicController ");
    This has solved my problem.


    Bunty...

Posting Permissions

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