karatelabs / karate

Test Automation Made Simple
https://karatelabs.github.io/karate
MIT License
8.09k stars 1.94k forks source link

MockSpringMvcServlet not respecting @ControllerAdvice NoHandlerFoundException #709

Closed jesimone57 closed 5 years ago

jesimone57 commented 5 years ago

The code to demonstrate this issue is available as a github project here:

https://github.com/jesimone57/spring_boot_rest_examples

Set up mvn clean compile test

All test pass except for one: `Results :

Tests in error: nourl.feature:11 - path: $['content-type'][0], actual: null, expected: 'application/json', reason: actual value is not a string

Tests run: 72, Failures: 0, Errors: 1, Skipped: 1`

Description: I just added the Karate mock servlet dependency to my project and followed the instructions for making use of it to test all the karate features without the need to run a spring-boot server. All the feature tests passed successfully (28 of them) except for 1.

What failed is a test that checks for a NoHandlerFoundException when an invalid URL is given. The expected response is { method: "GET", status_code: 404, uri_path: "/hello", error_message: "The URL you have reached is not in service at this time", errors: [ ] }

This test, of necessity, would depend on the following 2 application properties being set in the test environment (application.properties file)

# exception handling (see GlobalExceptionHandler handleNoHandlerFoundException method) spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false

This is according to the Spring documentation for handling NoHandlerFoundException.

The junit 4 version of this @SpringBootTest passes successfully and may be found in HelloWorldControllerExceptionTest. We get the expected response as given above. However, the nourl.feature file given as

`Feature: No URLfound proper error response

Background:

fails because no NoHandlerFoundException is ever thrown by the mock servlet container upon a request for a non-existent URL. The code for this handler is found in @ControllerAdvice annotated class GlobalExceptionHandler.

I added the following @Configuration @EnableAutoConfiguration @PropertySource("classpath:application.properties") as specified in the MockConfig from the karate mock servlet demo, but still fails.

What am I missing to make this work. Is this a karate bug or limitation.
Thanks.

jesimone57 commented 5 years ago

The code to demonstrate this issue is available as a github project here:

https://github.com/jesimone57/spring_boot_rest_examples

Set up mvn clean compile test

All test pass except for one: `Results :

Tests in error: nourl.feature:11 - path: $['content-type'][0], actual: null, expected: 'application/json', reason: actual value is not a string

Tests run: 72, Failures: 0, Errors: 1, Skipped: 1`

Description: I just added the Karate mock servlet dependency to my project and followed the instructions for making use of it to test all the karate features without the need to run a spring-boot server. All the feature tests passed successfully (28 of them) except for 1.

What failed is a test that checks for a NoHandlerFoundException when an invalid URL is given. The expected response is { method: "GET", status_code: 404, uri_path: "/hello", error_message: "The URL you have reached is not in service at this time", errors: [ ] }

This test, of necessity, would depend on the following 2 application properties being set in the test environment (application.properties file)

# exception handling (see GlobalExceptionHandler handleNoHandlerFoundException method) spring.mvc.throw-exception-if-no-handler-found=true spring.resources.add-mappings=false

This is according to the Spring documentation for handling NoHandlerFoundException.

The junit 4 version of this @SpringBootTest passes successfully and may be found in HelloWorldControllerExceptionTest. We get the expected response as given above. However, the nourl.feature file given as

`Feature: No URLfound proper error response

Background:

fails because no NoHandlerFoundException is ever thrown by the mock servlet container upon a request for a non-existent URL. The code for this handler is found in @ControllerAdvice annotated class GlobalExceptionHandler.

I added the following @Configuration @EnableAutoConfiguration @PropertySource("classpath:application.properties") as specified in the MockConfig from the karate mock servlet demo, but still fails.

What am I missing to make this work. Is this a karate bug or limitation.
Thanks.

jesimone57 commented 5 years ago

@ptrthomas please accept my apology for taking so long to write this up.