gpt4 book ai didi

java - 如何使用 JMockit 模拟 void 方法异常

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:10 26 4
gpt4 key购买 nike

我在尝试模拟 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com