【发布时间】:2021-01-07 20:22:29
【问题描述】:
我升级了org.jmockit:jmockit:1.31 -> org.jmockit:jmockit:1.34,但在尝试模拟异常时出现以下错误:Class java.lang.Throwable is not mockable
在 Jmockit 中进行了哪些更改以不再使模拟可抛出异常成为可能?
Java 11
JMockit 1.34
Testng 6.9.10
测试
@Test
public void HttpStatusWhenFailure(@Injectable CustomResponseException e) throws CustomException {
new Expectations() {{
ProxyManager.rrun((String) any); result = e;
e.getHttpStatus(); result = 500;
}};
Response response = resource.run(YearMonth.now().toString());
assertEquals(response.getStatus(), 500);
}
自定义异常
@ApplicationException
public class CustomException extends Exception {
private static final long serialVersionUID = 100L;
protected int httpStatus;
public CustomException(int httpStatus) {
super();
this.httpStatus = httpStatus;
}
public CustomException(int httpStatus, String message) {
super(message);
this.httpStatus = httpStatus;
}
public CustomException(int httpStatus, String message, Throwable cause) {
super(message, cause);
this.httpStatus = httpStatus;
}
public CustomException(int httpStatus, Throwable cause) {
super(cause);
this.httpStatus = httpStatus;
}
public int getHttpStatus() {
return httpStatus;
}
}
【问题讨论】:
标签: java testng java-11 jmockit