英文:
Line is not covered according to test coverage, despite successfully using verify and asserts
问题
我的目标是让Sonar检测到在MyService
类中抛出的错误(通过注释指出)实际上在测试中被覆盖了。然而,Sonar表示没有覆盖它。尽管成功验证了logAndThrowException
方法被调用,但Sonar仍然表示测试中未覆盖此行。
MyService类:
public class MyService {
@Autowired
RestTemplate restTemplate;
@Autowired
MyExceptionHandler myExceptionHandler;
public ResponseEntity doSomeRequest() {
try {
ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class);
if (responseEntity.getStatusCode() == HttpStates.OK) {
return response;
}
myExceptionHandler.logAndThrowException("error"); // Sonar says this line is not covered in tests
} catch (RestClientException e) {
// 处理异常
}
}
}
MyExceptionHandler类:
public class MyExceptionHandler {
public void logAndThrowException(String msg) throws MyCustomException {
// 记录一些日志
throw new MyCustomException();
}
}
测试代码:
@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
@Mock
private RestTemplate restTemplate;
@Mock
MyExceptionHandler myExceptionHandler;
@Mock
ResponseEntity responseEntity;
@InjectMocks
MyService myService = new MyService();
@Test
public void testFailedRequest() {
when(restTemplate.exchange(any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity);
when(responseEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
Mockito.doThrow(MyCustomException.class).when(myExceptionHandler).logAndThrowException(ArgumentMatchers.anyString());
assertThrows(MyCustomException.class, () -> myService.doSomeRequest());
Mockito.verify(myExceptionHandler, Mockito.times(1)).logAndThrowException(ArgumentMatchers.anyString());
}
}
英文:
My goal is for Sonar to detect that the error thrown in the MyService
class (pointed through the comment) is actually covered in the test. However, Sonar says it is not. Despite successfully verifying that the logAndThrowException
method is called, Sonar still says this line is not covered in the test.
MyService class:
public class MyService {
@Autowired
RestTemplate restTemplate;
@Autowired
MyExceptionHandler myExceptionHandler;
public ResponseEntity doSomeRequest() {
try {
ResponseEntity<String> response = restTemplate.exchange(requestEntity, String.class);
if (responseEntity.getStatusCode() == HttpStates.OK) {
return response;
}
myExceptionHandler.logAndThrowException("error"); // Sonar says this line is not covered in tests
} catch (RestClientException e) {
// Handle it
}
}
}
MyExceptionHandler class:
public class MyExceptionHandler {
public void logAndThrowException(String msg) throws MyCustomException {
// do some logs
throw new MyCustomException();
}
}
Test code:
@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
@Mock
private RestTemplate restTemplate
@Mock
MyExceptionHandler myExceptionHandler;
@Mock
ResponseEntity responseEntity;
@InjectMocks
MyService myService = new MyService();
@Test
public void testFailedRequest() {
when(restTemplate.exchange(any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity);
when(responseEntity.getStatusCode()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR);
Mockito.doThrow(MyCustomException.class).when(myExceptionHandler).logAndThrowException(ArgumentMatchers.anyString());
assertThrows(MyCustomException.class, () -> myService.doSomeRequest());
Mockito.verify(myExceptionHandler, Mockito.times(1)).logAndThrowException(ArgumentMatchers.anyString());
}
}
答案1
得分: 1
我相信我以前见过这个。问题在于它无法“完成”执行那行代码,因为在执行该行时抛出了异常。
英文:
I believe I've seen this before. The issue is that it doesn't "finish" executing that line, because an exception is thrown while executing that line.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论