5

I am trying to test my application with junit.

Therefore I've setup the following class:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/META-INF/spring/applicationContext-test.xml" )
@TransactionConfiguration
@Transactional
public class DispatcherServletTest extends AbstractJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private DispatcherServlet dispatcher;

    @Before
    public void setUp() throws Exception {
            request = new MockHttpServletRequest();
            response = new MockHttpServletResponse();

            MockServletConfig config = new MockServletConfig("myapp");
            config.addInitParameter("contextConfigLocation","classpath*:webmvc-config.xml");

            dispatcher = new DispatcherServlet();
            dispatcher.init(config);
    }
    //test cases

}

So the problem is, that it seems that my dispatcher servlet cannot send any request to any of my controllers.

I think that there is something with the configuration - contextConfigurationLocation. It looks like he can find the file (otherwise it would throw an exception) , but doesn't load any configuration

The logger says:

org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [http://localhost:8080/myapp/abc]

But I have absolutely no idea what's wrong...

I would appreciate any help!

Thanks in advance

Alexander
  • 7,178
  • 8
  • 45
  • 75
  • 1
    Do you really want to test the dispatcher servlet, why not test "only" the web controller handler methods? – Ralph Oct 20 '11 at 09:25
  • i think we can't test the http request using junit. only the service. – jaleel Oct 20 '11 at 09:38
  • No I would really test the whole application meaning if the correct controller method is called. For example: I send a POST to /myposturl with some data to the backend and want to check if the correct controller with the method responsible for this call is "answering" So what I thought is, that I have to send the request to the dispatcher servlet and not the controller directly. In my controller I just have the method handling this request: RequestMapping(method = RequestMethod.POST, headers = "Accept=...") public ResponseEntity createFromJsonArray(@RequestBody String json) {... – Alexander Oct 20 '11 at 10:39

2 Answers2

1

Mines are working fine, try the following tweaks.

  1. if you're using Junit4 no need to extend you test class, the junit runner should do the trick
  2. Load the context config via classpath, and make sure is accessible from the test classpath

    @ContextConfiguration(locations={"classpath:applicationContext-test.xml"})

  3. then just test the annotated controllers. I do it like this:


    @Test
    @Transactional
    public void testAnnotatedListUser() throws Exception {
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        AnnotationMethodHandlerAdapter handlerAdpt = new AnnotationMethodHandlerAdapter();
        request.setRequestURI("/you/URIhere");
        ModelAndView mav = handlerAdpt.handle(request, response, this.controller);
        assertEquals("Incorrect view name returned", "myexpectedviewname", mav.getViewName());
    }

maverick
  • 2,185
  • 2
  • 16
  • 22
0

There are several Problems in my question:

At first, it is not possible to extend the AbstractJUnit4SpringContextTests and use @RunWith(...), because it's the same.

At second, you should not use the dispatcherServlert, but an Handler by defining the handler in you application.xml and autowiring it in the test case via @Autowire private Handler handler...

Then everything should work fine!

Alexander
  • 7,178
  • 8
  • 45
  • 75