Java 测试 Spring ExceptionalHandler
Java Test for Spring ExceptionalHandler
我有一个带有控制器和服务的 springboot 项目。还有一个像 -
这样的 GlobalExceptionHandler
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handle(DataIntegrityViolationException e, WebRequest request) {
....
String requestPath = ((ServletWebRequest)request).getRequest().getRequestURI();
// I am using this requestPath in my output from springboot
...
}
}
谁能告诉我如何在我的单元测试中编写 mock class
((ServletWebRequest)request).getRequest().getRequestURI()
遗憾的是,Mockito 不支持替换最终方法。您可以使用其他模拟框架,例如 PowerMock。
在这种情况下,我更喜欢用受保护的方法消除模拟的需要:
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handle(final DataIntegrityViolationException e, final WebRequest request) {
final String requestPath = getRequestUri(request);
return ResponseEntity.ok().body(requestPath);
}
protected String getRequestUri(final WebRequest request) {
return ((ServletWebRequest) request).getRequest().getRequestURI();
}
}
和匿名class在测试中:
public class GlobalExceptionHandlerTests {
private final GlobalExceptionHandler handler = new GlobalExceptionHandler() {
@Override
protected String getRequestUri(final org.springframework.web.context.request.WebRequest request) {
return "http://localhost.me";
};
};
@Test
void test() throws Exception {
final ResponseEntity<Object> handled = handler.handle(new DataIntegrityViolationException(""),
null);
assertEquals("http://localhost.me", handled.getBody());
}
}
我有一个带有控制器和服务的 springboot 项目。还有一个像 -
这样的 GlobalExceptionHandlerpublic class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handle(DataIntegrityViolationException e, WebRequest request) {
....
String requestPath = ((ServletWebRequest)request).getRequest().getRequestURI();
// I am using this requestPath in my output from springboot
...
}
}
谁能告诉我如何在我的单元测试中编写 mock class
((ServletWebRequest)request).getRequest().getRequestURI()
遗憾的是,Mockito 不支持替换最终方法。您可以使用其他模拟框架,例如 PowerMock。
在这种情况下,我更喜欢用受保护的方法消除模拟的需要:
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handle(final DataIntegrityViolationException e, final WebRequest request) {
final String requestPath = getRequestUri(request);
return ResponseEntity.ok().body(requestPath);
}
protected String getRequestUri(final WebRequest request) {
return ((ServletWebRequest) request).getRequest().getRequestURI();
}
}
和匿名class在测试中:
public class GlobalExceptionHandlerTests {
private final GlobalExceptionHandler handler = new GlobalExceptionHandler() {
@Override
protected String getRequestUri(final org.springframework.web.context.request.WebRequest request) {
return "http://localhost.me";
};
};
@Test
void test() throws Exception {
final ResponseEntity<Object> handled = handler.handle(new DataIntegrityViolationException(""),
null);
assertEquals("http://localhost.me", handled.getBody());
}
}