Spring has 2 setups for the MockMvc:
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?
Technically speaking, tests using MockMvc are in the boundaries between unit and integration tests. They aren't unit tests because endpoints are tested in integration with a Mocked MVC container with mocked inputs and dependencies.
MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.
React Full Stack Web Development With Spring BootUnit Testing is a one of the testing done by the developers to make sure individual unit or component functionalities are working fine. In this tutorial, we are going to see how to write a unit test case by using Mockito and Web Controller.
if you are writing unit tests a recommend you use @Mock and @InjectMocks . But if you really want test all the flow and need to inject classes, you can @RunWith(SpringJUnit4ClassRunner. class) and @Autowired your classes.
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:
The "webAppContextSetup" loads the actual Spring MVC configuration resulting in a more complete integration test. Since the TestContext framework caches the loaded Spring configuration, it helps to keep tests running fast even as more tests get added. Furthermore, you can inject mock services into controllers through Spring configuration, in order to remain focused on testing the web layer.
...
The "standaloneSetup" on the other hand is a little closer to a unit test. It tests one controller at a time, the controller can be injected with mock dependencies manually, and it doesn’t involve loading Spring configuration. Such tests are more focused in style and make it easier to see which controller is being tested, whether any specific Spring MVC configuration is required to work, and so on. The "standaloneSetup" is also a very convenient way to write ad-hoc tests to verify some behavior or to debug an issue.
...
Just like with integration vs unit testing, there is no right or wrong answer. Using the "standaloneSetup" does imply the need for some additional "webAppContextSetup" tests to verify the Spring MVC configuration. Alternatively, you can decide to write all tests with "webAppContextSetup" and always test against actual Spring MVC configuration.
...
The options provided in Spring MVC Test are different stops on the scale from classic unit to full integration tests. To be sure none of the options in Spring MVC Test are classic unit tests but they are a little closer to it. For example you can isolate the service layer with mocks injected into controllers and then you’re testing the web layer only through the DispatcherServlet and with actual Spring configuration, just like you might test the database layer in isolation of the layers above. Or you could be using the standalone setup focusing on one controller at a time and manually providing the configuration required to make it work.
When in doubt, I suggest first reading the reference manual before posting questions here. ;)
Regards,
Sam (author of the Spring TestContext Framework)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With