Wolf359
Wolf359

Reputation: 2715

Spring Boot Test with Mockito : @Validated annotation is being ignored during unit tests

I'm using Spring Boot 2.1.1, JUnit 5, Mockito 2.23.4.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>

Here's my controller :

@RestController
@Validated
public class AramaController {

    @ResponseStatus(value = HttpStatus.OK)
    @GetMapping("/arama")
    public List<Arama> arama(@RequestParam @NotEmpty @Size(min = 4, max = 20) String query) {
        return aramaService.arama(query);
    }
}

This controller works as expected.

curl with no "query" parameter returns Bad Request 400 :

~$ curl http://localhost:8080/arama -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /arama HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 400 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Length: 0
< Date: Wed, 12 Dec 2018 21:47:11 GMT
< Connection: close
< 
* Closing connection 0

curl with "query=a" as parameter returns Bad Request 400 as well :

~$ curl http://localhost:8080/arama?query=a -v
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /arama?query=a HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 400 
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Wed, 12 Dec 2018 21:47:33 GMT
< Connection: close
< 
* Closing connection 0
{"message":"Input error","details":["size must be between 4 and 20"]}

This controller and validation works flawlessly when running on a server.

During unit tests the @Validated annotation doesn't seem to have any effect.

Here my test code :

@ExtendWith(MockitoExtension.class)
class AramaControllerTest {

    @Mock
    private AramaService aramaService;

    @InjectMocks
    private AramaController aramaController;

    private MockMvc mockMvc;

    @BeforeEach
    private void setUp() {
        mockMvc = MockMvcBuilders
                .standaloneSetup(aramaCcontroller)
                .setControllerAdvice(new RestResponseEntityExceptionHandler())
                .build();

    }

    @Test
    void aramaValidationError() throws Exception {

        mockMvc
                .perform(
                        get("/arama").param("query", "a")
                )
                .andExpect(status().isBadRequest());

        verifyNoMoreInteractions(aramaService);
    }
}

This test results in failure :

java.lang.AssertionError: Status expected:<400> but was:<200>
Expected :400
Actual   :200

Since the @Valid annotations pass my other test cases, and they work without loading the Spring context, is there a way to make the @Validated annotation work as well with Mockito (again, without loading the Spring context) ?

Upvotes: 1

Views: 4142

Answers (2)

Wolf359
Wolf359

Reputation: 2715

I got the answer elsewhere and wanted to share :

Without starting up the context, you won't have @Validator getting tested because validator instances are Spring beans. However, @Valid will work as it is a JSR-303 standard.

As of now, what I can suggest is.

@SpringBootTest
@ExtendWith(SpringExtension.class)

Upvotes: 2

Chi Dov
Chi Dov

Reputation: 1527

maybe you can try using @WebMvcTest and add SpringExtension

@ExtendWith({SpringExtension.class, MockitoExtension.class})
@WebMvcTest(AramaController.class)
class AramaControllerTest {

    @Mock
    private AramaService aramaService;

    @InjectMocks
    private AramaController aramaController;

    @Autowired
    private MockMvc mockMvc;

    @Test
    void aramaValidationError() throws Exception {

        mockMvc
                .perform(
                        get("/arama").param("query", "a")
                )
                .andExpect(status().isBadRequest());

        verifyNoMoreInteractions(aramaService);
    }
}

Upvotes: 0

Related Questions