Using Spring Mock MVC

I've seen people around me using Spring MVC in unit tests for controller classes, which doesn't help with that for unit test.

Unit tests should validate your actual implementation of the controller class, and this can be achieved more accurately with simple Junit tests rather than Spring Mock MVC.

But then the question is, what is the real use of Spring Mock MVC? Why do you need it?

Let's say I have the code below:

@Controller
@RequestMapping("/systemusers")
public class SystemUserController
{
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String getUser(final Model model)
    {

        // some logic to return user details as json

        return UserDetailsPage;
    }
 }

      

I can validate this class / controller more accurately with Junit than with Spring Mock MVC (all it does is generate some json that can be asserted with junit).

I can also check with Spring Mock MVC, for example using the correct endpoint, returns the correct HTTP status and the correct response page string.

But doesn't that mean that we are testing Spring MVC functionality and not the actual code of the method under test?

PS: I've kept the code to the bare minimum, which I think is sufficient to explain my question. Let's assume there is no compilation error.

+3


source to share


2 answers


When it comes to unit testing classes Controller

(or whatever endpoint that is displayed), there are two things we will be testing:

(1) The actual controller logic itself is autonomous, i.e. correct service calls are called or not, etc.

(2) Request url and response status and object

Item (1) above is what we are testing in general with all other classes like Services, Utility classes, etc.



Item (2) needs to be further checked / checked for endpoints (controller classes) that have been compromised , so whether we use Spring MockMVC

or other mechanisms to do this is really up to us.

Spring MockMVC

really helps us to start the servlet container in memory
and check that the correct controller methods are called, and then the correct answers appear.

From my personal experience, testing controllers (for element (2)) helped me resolve URL mapping issues (within the same controller of course), etc. directly, rather than committing them at later stages of the project.

+2


source


Based on my experience, I will try to answer your question.

First of all, we need to understand why we are using unit testing?

This is an additional check used by the developer to write clean, working code. Clean working code means that every line written should do what it is expected to do. how do you achieve this? This is where unit testing comes in. a stand-alone unit of code that you write must be tested separately. The method is best in code that is a separate device code template.

Testing Units of Measure for a Method



The developer must write a test for the method that describes the method's behavior. And the possible checks that I follow are this returning the expected value given all positive scenarios? does it work in case of an exception? is it calling the correct subsequent methods?

The developer must test the method by actually calling it by providing a mock environment

Below is the answer to your question. although it is solely based on developers.

Controller methods are designed to invoke the correct call input and return a value from the service for viewing. So I can think of writing a unit test case for a controller method, which you think is the right approach. But you have to test the method by calling it in the same way as it will be called in real time. so you need to call the controller method just like MVC. Hence, it is better to use MockMVC. MockMVC is also useful for inspecting URLs, input parameters, response and state that are part of a controller method. With all this in mind, it makes it a stand-alone unit of code.

Hope this clears up your doubts.

0


source







All Articles