sab
sab

Reputation: 5052

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.

Upvotes: 0

Views: 611

Answers (2)

nrainer
nrainer

Reputation: 2633

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

Upvotes: 0

Betlista
Betlista

Reputation: 10547

Correct me if I misunderstood something, you are doing JUnit test and then you are checking coverage as part of the test? It seems like manual step to me, which is something you do not want in JUnit testing, right?

In the following example I used EasyMock, but you can use whatever you prefer ;-)

@Test
public void testGetters() {
    Bean bean = EasyMock.createMock(Bean.class);
    // set your expectations
    EasyMock.expect(bean.getA()).andReturn(0).times(1);
    EasyMock.replay(bean);

    // use the bean
    bean.getA(); // if this is commented, test fails

    // verify your expectations
    EasyMock.verify(bean);
}

Is this similar to what you want to achieve?

I used maven project and my dependencies are

<dependencies>

    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.3.1</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

</dependencies>

Upvotes: 0

Related Questions