The biggest myth of quality code : 100% Test coverage

How many of you believe you have quality code because you have good test coverage? Or how many of you feel you are working with bad quality legacy code because it doesn’t have any test coverage? Recently every organization started focusing on quality especially automation. Started putting people to increase unit test coverage. But did you question : Did you gain anything out of it? Is your code quality improved? How long these test cases will be relevant ? Do you think developers would be comfortable modifying it when code changing?

Following simple calculator with responsibility to do 3 basic operations. Both are working and having 100% test coverage. Can you guess which is good and why?

Implementation 1

public int calculate(String operation, int arguement1,int arguement2){
        if(operation.equals("Addition")){
            return arguement1 + arguement2;
        }else if(operation.equals("Substraction")){
            return arguement1 - arguement2;
        }else if(operation.equals("Multiplication")){
            return arguement1 * arguement2;
        }
        return 0;
    }

    @Test
    public void testCalculator() throws Exception {
        assertEquals(new Calculator().calculate("Addition", 10, 5),15);
        assertEquals(new Calculator().calculate("Multiplication", 10, 5),50);
        assertEquals(new Calculator().calculate("Substraction", 10, 5),5);
    }

Implementation 2

public int add(int arguement1, int arguement2) {
        try{
            return arguement1 + arguement2;
        }catch(NullPointerException nullPointerException){
            throw new RuntimeException("Arguement shouldn't be null");
        }
    }

    public int substraction(int arguement1, int arguement2) {
        return arguement1 - arguement2;
    }

    public int multiplication(int arguement1, int arguement2) {
        return arguement1 * arguement2;
    }

    @Test
    public void shouldAddTwoValues() throws Exception {
        assertThat(new Calculator2().add(10, 5), is(15));
    }
    
    @Test
    public void shouldSubstractSecondFromFirstValue() throws Exception {
        assertThat(new Calculator2().substraction(10, 5), is(5));
    }
    
    @Test
    public void shouldMultiplyTwoValues() throws Exception {
        assertThat(new Calculator2().multiplication(10, 5), is(50));
    }
    
    @Test(expected=RuntimeException.class)
    public void shouldThrowExceptionIfNullArguement() throws Exception {
        Integer nullInt = null;
        new Calculator2().add(nullInt, 5);
    }
  • Unit test should act like documentation : In second implementation class and methods themselves document what they do. Test cases further talk about edge case behavior. Ideally they should be written before actual code
  • In first code, there is a need for KT for people consuming this service as “Addition” has other similar words like “Add” or “addition”. Second one avoids it completely

Well there can be more. What I want to say here is simple, do not aim for test coverage but aim for good quality code which can be achieved by following TDD or at least writing some good test cases.

Advertisements

One thought on “The biggest myth of quality code : 100% Test coverage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s