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

时间:2017-05-23 04:57:22

标签: java unit-testing spring-test jmockit

尝试模拟void方法异常时遇到问题。

    @Test
public void throwInternalServerException() throws Exception {
    new NonStrictExpectations() {{
        mockedClassInstance.voidMethod("abc", "def");
        result = new ServiceException();
    }};

    mockMvc.perform(post("/someRestApi/"))
            .andExpect(status().isInternalServerError());

在上面的代码中,如果我用 anyString

替换params “abc”“def”,它会成功

我尝试了this question的解决方案,但它不起作用。

任何解决方案?

我使用了库:jmockit和spring-test。

1 个答案:

答案 0 :(得分:0)

关键字anystring用于场景中,无论参数是什么(在这种情况下,' abc'' def'),它都会抛出异常

相关问题