Announcement Announcement Module
Collapse
No announcement yet.
Problem testing Controllers classes :( Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem testing Controllers classes :(

    Is possible to load an instance of the ConfigurableWebApplicationContext using a class similar as FileSystemXmlApplicationContext ??

    It's that I want:
    Code:
    //ConfigurableWebApplicationContext root = new FileSystemXmlApplicationContext("web/WEB-INF/context.xml);
    I'm trying to use the Raiable's example below, to test Controllers classes within Eclipse.
    http://www.jroller.com/page/raible/20040423
    But, the relative paths like "/WEB-INF/application-Context.xml" is not working. That is throwing a FileNotFoundException.

    Best regards,

    Franklin Samir
    http://www.portaljava.com

  • #2
    This should work.

    Note from FileSystemXmlApplicationContext JavaDocs:
    Treats resource paths as file system resources, when using getResource. Resource paths are considered relative to the current VM working directory, even if they start with a slash.

    Comment


    • #3
      I already saw that JavaDocs, but I didn't found a way to use FileSystemXmlApplicationContext to instance a ConfigurableWebApplicationContext.

      Comment


      • #4
        Sorry about that. I thought you were trying to solve the "FileNotFoundException".

        You can't use FileSystemXmlApplicationContext as it doesn't implement ConfigurableWebApplicationContext. Why not just copy Matt's example.

        Comment


        • #5
          Hi,

          I've copied the Matt's example, but that is throwing the FileNotFoundException. I'm sure that the file is there, but the line below cannot find the file.
          Code:
          ConfigurableWebApplicationContext.setConfigLocations(new String[] {"web/WEB-INF/applicationContext.xml"});
          I think I'm doing something wrong... grrrr

          Explaning again:

          That's doesn't work for me (that is my Controller's test case):
          Code:
           
           ConfigurableWebApplicationContext root = new XmlWebApplicationContext();
                  MockServletContext sc = new MockServletContext("");
                  root.setServletContext(sc);
                  root.setConfigLocations(new String[] {"web/WEB-INF/applicationContext.xml"});
          But that is working( my Business Objects test cases):
          Code:
           
           AbstractApplicationContext ac = null;
           ac = new FileSystemXmlApplicationContext( new String[] {"web/WEB-INF/applicationContext.xml"});
          Thank you.

          Franklin

          Comment


          • #6
            You can test your controllers like this:
            1) Make a base test class, extending AbstractDependencyInjectionSpringContextTests (or AbstractTransactionalSpringContextTests)
            2) Override getConfigLocations to load you application context
            3) Write your controller test class, extending your base test class
            4) Write your test (see below for an example)

            Code:
                public void testAddBindAndExists() throws Exception {
                    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/myURL.htm");
                    request.addParameter("username", "jj");//"jj" is a username already in use
                    //...add rest of parameters here
                    MyForm myForm = (MyForm) this.applicationContext.getBean("myForm");
                    myForm.setSessionForm(false);//You need to set this if it was set to true
            
                    MockHttpServletResponse response = new MockHttpServletResponse();
                    ModelAndView mv = myForm .handleRequest(request, response);
                    BindException errors = (BindException) mv.getModel().get(BindException.ERROR_KEY_PREFIX+"command");
                    
                    assertNotNull(errors);
                    assertTrue("Should be duplicate username", errors.hasFieldErrors("username"));
                    boolean foundMatch = false;
                    String codes[] = errors.getFieldError("username").getCodes();
                    for &#40;int i=0; i<codes.length; i++&#41; &#123;
                        foundMatch = "duplicate".equals&#40;codes&#91;i&#93;&#41;;
                        if &#40;foundMatch&#41;
                            break;
                    &#125;
                    assertTrue&#40;foundMatch&#41;;
                &#125;

            Comment


            • #7
              Same problem here.
              Code:
              protected XmlWebApplicationContext ctx;
              	
              	@Override
              	protected void setUp() {
              		String[] paths = {"/WEB-INF/applicationContext.xml",
              		"/WEB-INF/dispatcher-servlet.xml"};
              		
              		ctx = new XmlWebApplicationContext();
              		MockServletContext m = new MockServletContext("");		
              		ctx.setServletContext(m);		
              		ctx.setConfigLocations(paths);
              		ctx.refresh();
              		
              	}
              Doesn't work with configuration xmls in:
              WebRoot/WEB-INF/applicationContext.xml

              but if I move applicationContext.xml to WEB-INF/classes and change to:
              String[] paths = {"applicationContext.xml"}

              I start finding them.

              Comment

              Working...
              X