Neigaard
Neigaard

Reputation: 4074

Calling controller from junit test

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

Upvotes: 1

Views: 3568

Answers (2)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 299218

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:

Upvotes: 4

oksayt
oksayt

Reputation: 4365

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.

Upvotes: 0

Related Questions