Java 我如何处理Mockito中的异常?

Java 我如何处理Mockito中的异常?,java,junit,exception-handling,mockito,Java,Junit,Exception Handling,Mockito,我想在我的方法中测试异常处理。。 我试着去做show()和willThrow() 这是我的测试代码 这是我的方法 catch (Throwable e) { JFLogger.logger(getClass()).error("add() ", e); try { response.put("result", "failed"); response.put("result_cod

我想在我的方法中测试异常处理。。 我试着去做show()和willThrow()

这是我的测试代码

这是我的方法

catch (Throwable e) {

            JFLogger.logger(getClass()).error("add() ", e);

            try {

                response.put("result", "failed");
                response.put("result_code", "exception");
                JFLogger.logger(getClass()).debug("{}", response.toString());

            } catch (Throwable th) {
            }
        }
        return response.toString();
这个案子结束了

但是

given(landlineCodeDAO.findByCode(anyString())).willThrow(new Throwable("Exception occurred by TestCase"));

String resultResponse = ws.delete(memberUid, partitionUid, landlineCodeIn);

assertThat(resultResponse, is("{\"result123123\":\"fail123123ed\",\"res123ult_code\":\"excepti123123on\"}"));
它也完成了

如何检查结果响应


我是一个年轻的韩国学生,我的英语不好。。
所以,请理解这个问题。

您是说这两个测试(一个预期正确的结果,另一个预期插入垃圾字符的结果不正确)执行时没有错误吗?如果在每个测试中调用System.out.println(resultResponse)(仅用于调试测试),您会看到什么?我只能看到“进程已完成,退出代码为0”。。。我看不到System.out.println(resultResponse)的结果,因为“给定(landlineCodeDAO.findByCode(anyString())).willThrow(新的Throwable(“TestCase发生异常”);“不起作用……您是否在JUnit中运行测试?是的,我使用JUnit的代码很难理解您的问题,因此,我将尝试重新表述我认为我理解的内容:在JUnit中运行2个测试(听起来像是从IntelliJ启动的)。其中一个应该成功并且成功,另一个在
assertThat
中包含错误并且应该失败但没有失败。
given(landlineCodeDAO.findByCode(anyString())).willThrow(new Throwable("Exception occurred by TestCase"));

String resultResponse = ws.delete(memberUid, partitionUid, landlineCodeIn);

assertThat(resultResponse, is("{\"result123123\":\"fail123123ed\",\"res123ult_code\":\"excepti123123on\"}"));