javaintellij-ideatest-coveragearrange-act-assert

code-coverage of the getter through the assert


While unit-testing, is there a way to enable code-coverage analysis only during some steps?

I wish to measure the code-coverage of the assert part of a test. The tools that I currently use don't make the difference if the line is executed during the Action-part of the test, or during the assert-part.

Accordingly, I cannot check if all the getter of my beans are read by the assert method. Ideally I want to activate the covering measure only during the execution of some of my methods.

A sample of code:

//method to test
void runToTest(Bean myBean){
    myBean.setA(1);
    myBean.setB(2);
    aString=myBean.getA()+myBean.getB();
}

@Test
void should_check_all_field(){
    myBean=new Bean()
    myService.runToTest(myBean);
    assertMethode();

 }
void assertMethod(){
     Assert.assertNotNull(myBean.getA())
}

Currently, the tools I use report than getA and getB are covered. I want a way to detect that getB hasn't been read by the assertMethod.

I use Java and IntelliJ and my test class follows the Arrange-Act-Assert syntax.


Solution

  • No, I am not aware of any tool that supports this functionality.

    The code coverage expresses which portion of your source code was run through by the tests. By checking the result of a get method you are testing also the correctness of the getter. Thus, a test checks more than what is directly covered by assertion statements.

    What you are probably looking for is mutation testing. You want to see if the code is not just executed by the test but as well if the test is capable of detecting an incorrect behaviour of the code. Mutation testing introduces faults into the source code and analyzes whether the tests can reveal these.

    See also: https://stackoverflow.com/a/27400613/584532