Spring has 2 setups for the MockMvc:
- Standalone setup
- WebApplicationContext setup
In general what kind of testing is MockMvc used for? Unit or Integration? or Both?
Am i right in saying that using the standalone setup (running outside the Spring's application context) allows you to write unit tests and with the WebApplicationContext setup you can write integration tests?
Both forms are actually integration tests since you are testing the integration of your code with the Spring
DispatcherServlet
and supporting infrastructure. The difference lies in the amount of supporting infrastructure that is used behind the scenes.The details are documented in the Spring reference manual.
Noteworthy excerpts:
...
...
...
When in doubt, I suggest first reading the reference manual before posting questions here. ;)
Regards,
Sam (author of the Spring TestContext Framework)
I would say that both methods are for integration testing, but standalone force you to specify which controller you are testing.
WebApplicationContext setup is loading whole context, so you don't care where is specific controller which serves for example
/people
POST requests.So I would recommend using WebApplicationContext setup for testing your REST API in terms of interface which application need to work with. You dont couple test with actual code then + you are documenting the way the app should behave.