Line is not covered according to test coverage, despite successfully using verify and asserts

huangapple go评论71阅读模式
英文:

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&lt;String&gt; response = restTemplate.exchange(requestEntity, String.class);

			if (responseEntity.getStatusCode() == HttpStates.OK) {
				return response;
			}

			myExceptionHandler.logAndThrowException(&quot;error&quot;); // 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, () -&gt; 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.

huangapple
  • 本文由 发表于 2020年10月16日 18:03:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64387045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定