Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用JMockit模拟void方法异常_Java_Unit Testing_Spring Test_Jmockit - Fatal编程技术网

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

Java 如何使用JMockit模拟void方法异常,java,unit-testing,spring-test,jmockit,Java,Unit Testing,Spring Test,Jmockit,我在尝试模拟void方法异常时遇到问题 @Test public void throwInternalServerException() throws Exception { new NonStrictExpectations() {{ mockedClassInstance.voidMethod("abc", "def"); result = new ServiceException(); }}; mockMvc.perform

我在尝试模拟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”,则会成功

我尝试了解决问题的方法,但不起作用

有解决办法吗


我使用了library:jmockit和spring test。

在场景中使用关键字
anystring
,无论参数是什么(在本例中为'abc'和'def'),它都会抛出异常。

在场景中使用关键字
anystring
,无论参数是什么(在本例中为'abc'和'def'),它将抛出异常

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

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