Java Mockito ArgumentCaptor不';t捕捉块中的泥质
我写了这个服务Java Mockito ArgumentCaptor不';t捕捉块中的泥质,java,junit,mockito,Java,Junit,Mockito,我写了这个服务 public class FirstService { private final SecondService secondService; public FirstService(SecondService secondService) { this.secondService = secondService; } public void hz() throws Exception { try {
public class FirstService {
private final SecondService secondService;
public FirstService(SecondService secondService) {
this.secondService = secondService;
}
public void hz() throws Exception {
try {
methodThrowsException();
} catch (Exception e){
secondService.handleErrorMessage(e.getMessage());
throw e;
}
}
private void methodThrowsException() throws Exception {
throw new Exception("message");
}
}
这项服务:
public class SecondService {
public void handleErrorMessage(String message) {}
}
我需要验证是否调用了handleErrorMessage
。我写了一个测试:
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
public class FirstServiceTest {
private FirstService firstService;
private SecondService secondService;
@Before
public void setUp() {
secondService = mock(SecondService.class);
firstService = new FirstService(secondService);
}
@Test(expected = Exception.class)
public void hz() throws Exception {
firstService.hz();
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
verify(secondService).handleErrorMessage(argumentCaptor.capture());
String value = argumentCaptor.getValue();
assertEquals("message", value);
}
}
import org.junit.Before;
导入org.junit.Test;
导入org.mockito.ArgumentCaptor;
导入静态org.junit.Assert.*;
导入静态org.mockito.mockito.mock;
导入静态org.mockito.mockito.verify;
公共类FirstServiceTest{
私人优先服务优先服务;
私人借调服务;
@以前
公共作废设置(){
secondService=mock(secondService.class);
firstService=新的firstService(第二个Service);
}
@测试(预期=异常.class)
public void()引发异常{
firstService.hz();
ArgumentCaptor ArgumentCaptor=ArgumentCaptor.forClass(String.class);
验证(secondService).handleErrorMessage(argumentCaptor.capture());
字符串值=argumentCaptor.getValue();
assertEquals(“消息”,值);
}
}
通过测试。但是如果我更改assertEquals(“message666”,value)代码>它仍然通过。如果我没有在catch block中抛出异常,ArgumentCaptor将捕获参数,但当我抛出异常时,它将不起作用。您的测试将被注释:
@Test(预期=Exception.class)
这意味着如果异常
(或的任何子类)达到顶层,测试将通过。这发生在测试的第一行:
firstService.hz();
这就是它过去的原因。不幸的是,该异常意味着测试的其余部分永远不会运行,因为该异常会向上传播并超出测试方法
有点难看,但这段代码符合您的要求:
@Test
public void hz() throws Exception {
try {
firstService.hz();
// If we get here, then we didn't throw an exception - fail
Assert.fail();
} catch (Exception ex) {
// Exception was expected - disregard and continue
// no-op
}
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
verify(secondService).handleErrorMessage(argumentCaptor.capture());
String value = argumentCaptor.getValue();
assertEquals("message", value);
}
@测试
public void()引发异常{
试一试{
firstService.hz();
//如果我们到达这里,那么我们没有抛出异常-失败
Assert.fail();
}捕获(例外情况除外){
//应出现异常-忽略并继续
//无操作
}
ArgumentCaptor ArgumentCaptor=ArgumentCaptor.forClass(String.class);
验证(secondService).handleErrorMessage(argumentCaptor.capture());
字符串值=argumentCaptor.getValue();
assertEquals(“消息”,值);
}
上面的方法运行您的方法,并捕获异常(如果您没有获得预期的异常,则会失败)。然后,它继续进行,并运行其余的测试
JUnit 5提供了一种稍微干净的方法,但您必须迁移:
@Test
public void hz() throws Exception {
Assertions.assertThrows(Exception.class, () -> firstService.hz());
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
verify(secondService).handleErrorMessage(argumentCaptor.capture());
String value = argumentCaptor.getValue();
assertEquals("asdf", value);
}
@测试
public void()引发异常{
assertThrows(Exception.class,()->firstService.hz());
ArgumentCaptor ArgumentCaptor=ArgumentCaptor.forClass(String.class);
验证(secondService).handleErrorMessage(argumentCaptor.capture());
字符串值=argumentCaptor.getValue();
资产质量(“asdf”,价值);
}
我在try-catch块中捕获异常,并在调用handleErrorMessage
方法后重新播放。是的-您的重新播放会一直播放到顶层,并通过firstService.hz()播放。尝试删除(预期=)
,您将在测试输出中看到它。(我复制并运行了您的代码:))嗨,PavelPetrashov,重点是firstService.hz()。不管你在这个方法中做了什么,最终你都会抛出一个异常。调用firstService.hz()时在测试类中;它将引发异常,因此不会执行下面的代码。正如Thalex所说,您的测试通过了,因为它期望@test(expected=Exception.class)出现异常