views:

36

answers:

2

Hi

How can I call a spring annotated controller from a JUnit test, in a way so that spring comes into play with binding and all, but without making a http request (just mocking out the request object)? It has to include the whole shebang from the controller and down, with JPA and database and all. We are also using EJB, so maybe a bean can do this for me?

The reason for this, is that I would like to have some automatic tests that tests performance of specific controller calls, but without the client and network traffic.

Thank you

+3  A: 

There's a section in the Spring reference about Unit-testing Spring MVC.

Here's the relevant excerpt:

Unit testing Spring MVC Controllers
To test your Spring MVC Controllers, use ModelAndViewAssert combined with MockHttpServletRequest, MockHttpSession, and so on from the org.springframework.mock.web package.

Reference:

seanizer
A: 

You can specify a test-specific spring context for your unit test like this:

@ContextConfiguration(locations = "classpath:spring/ITestAssembly.xml")

You can then make this context use a mock or embedded data source instead of the real database.

oksayt