-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #931 from Nishant-sehgal/develop
MockSpringMvcServlet issue for @ControllerAdvice NoHandlerFoundException
- Loading branch information
Showing
7 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
karate-demo/src/main/java/com/intuit/karate/demo/exception/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.intuit.karate.demo.exception; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class ErrorResponse { | ||
|
||
@JsonProperty("status_code") | ||
private int code; | ||
@JsonProperty("uri_path") | ||
private String path; | ||
private String method; | ||
@JsonProperty("error_message") | ||
private String message; | ||
|
||
public ErrorResponse() { | ||
} | ||
|
||
public ErrorResponse(int code, String path, String method, String message) { | ||
this.code = code; | ||
this.path = path; | ||
this.method = method; | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* @return the code | ||
*/ | ||
public int getCode() { | ||
return code;} | ||
|
||
/** | ||
* @param code the code to set | ||
*/ | ||
public void setCode(int code) { | ||
this.code = code;} | ||
|
||
/** | ||
* @return the path | ||
*/ | ||
public String getPath() { | ||
return path;} | ||
|
||
/** | ||
* @param path the path to set | ||
*/ | ||
public void setPath(String path) { | ||
this.path = path;} | ||
|
||
/** | ||
* @return the method | ||
*/ | ||
public String getMethod() { | ||
return method;} | ||
|
||
/** | ||
* @param method the method to set | ||
*/ | ||
public void setMethod(String method) { | ||
this.method = method;} | ||
|
||
/** | ||
* @return the message | ||
*/ | ||
public String getMessage() { | ||
return message;} | ||
|
||
/** | ||
* @param message the message to set | ||
*/ | ||
public void setMessage(String message) { | ||
this.message = message;} | ||
|
||
|
||
} | ||
|
38 changes: 38 additions & 0 deletions
38
karate-demo/src/main/java/com/intuit/karate/demo/exception/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.intuit.karate.demo.exception; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.context.request.ServletWebRequest; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
/** | ||
* @author nsehgal | ||
* | ||
*/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
|
||
/** | ||
* Adding these properties will make the following code active: | ||
* spring.mvc.throw-exception-if-no-handler-found=true | ||
* spring.resources.add-mappings=false | ||
* | ||
* @param ex | ||
* @param headers | ||
* @param status | ||
* @param webRequest | ||
* @return | ||
*/ | ||
@Override | ||
protected ResponseEntity<Object> handleNoHandlerFoundException(NoHandlerFoundException ex, HttpHeaders headers, HttpStatus status, WebRequest webRequest) { | ||
String uriPath = webRequest.getDescription(false).substring(4); | ||
String message = "The URL you have reached is not in service at this time"; | ||
String method = ((ServletWebRequest) webRequest).getRequest().getMethod(); | ||
ErrorResponse errorResponse = new ErrorResponse(status.value(), uriPath, method, message); | ||
return new ResponseEntity<>(errorResponse, status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
spring.jackson.default-property-inclusion=NON_NULL | ||
spring.jackson.default-property-inclusion=NON_NULL | ||
spring.mvc.throw-exception-if-no-handler-found=true | ||
spring.resources.add-mappings=false |
13 changes: 13 additions & 0 deletions
13
karate-demo/src/test/java/demo/error/NoUrlErrorRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package demo.error; | ||
|
||
import com.intuit.karate.KarateOptions; | ||
import demo.TestBase; | ||
|
||
/** | ||
* | ||
* @author nsehgal | ||
*/ | ||
@KarateOptions(features = {"classpath:demo/error/no-url.feature"}) | ||
public class NoUrlErrorRunner extends TestBase { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Feature: No URLfound proper error response | ||
|
||
Background: | ||
* url demoBaseUrl | ||
* configure lowerCaseResponseHeaders = true | ||
|
||
Scenario: Invalid URL response | ||
Given path '/hello' | ||
When method get | ||
Then status 404 | ||
And match header content-type contains 'application/json' | ||
And match header content-type contains 'charset=UTF-8' | ||
And match response.status_code == 404 | ||
And match response.method == 'GET' | ||
And match response.error_message == 'The URL you have reached is not in service at this time' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters