我在尝试模拟 void 方法异常时遇到问题。
@Test
public void throwInternalServerException() throws Exception {
new NonStrictExpectations() {{
mockedClassInstance.voidMethod("abc", "def");
result = new ServiceException();
}};
mockMvc.perform(post("/someRestApi/"))
.andExpect(status().isInternalServerError());
在上面的代码中,如果我将参数“abc”和“def”替换为anyString,则成功
我尝试了this question的解决方案但它不起作用。
有什么解决办法吗?
我使用了库:jmockit 和 spring-test。
最佳答案
场景中使用关键字anystring
,无论参数是什么(在本例中为“abc”和“def”),都会抛出异常。
关于java - 如何使用 JMockit 模拟 void 方法异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44126091/