gpt4 book ai didi

java - 检查异常对此方法无效

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:39:25 25 4
gpt4 key购买 nike

<分区>

我有以下类(class)在 StackOverflow 中有一个答案,但它处理列表 throw checked Exceptions from mocks with Mockito .我喜欢研究这种情况。找不到我想念的地方。

public SimpleClass{                                                         
private SimpleClass() {}
public void runMethod(request,String,Map,Object,Object) {
try {
doesSomething()....
}
}
catch(Exception e) {
String message = "" + request.getAttribute(X) + "Some message";
Logger.Log(param1 + param2 + message);
}
}

我的测试方法如下所示。我尝试使用 JUnit 运行覆盖但未覆盖 Catch block ,因此编写了以下测试方法。它抛出以下异常。无法到达我失踪的地方。

public class SimpleClassTest{                                               
@Test
public void testCatchBlock() {
SimpleClass instanceObj = PowerMockito.mock(SimpleClass.class);
Mockito.doThrow(new Exception())
.when(instanceObj)
.runMethod(request, anyString(), anyMap(), anyObject(), anyObject());
}
}

抛出异常

org.mockito.exceptions.base.MockitoException: 
Checked exception is invalid for this method!
Invalid: java.lang.Exception

编辑

我可以通过给出 NullPointerException 来运行该方法。当我尝试使用 Junit 进行代码覆盖时,catch block 完全显示为红色,而 catch 短语显示为黄色。如何实现 100% 的覆盖率以及如何在 catch block 中测试 String 消息。

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