Test overkills

I’ve seen kind of many times lately the following “pattern”;
Suppose we have a simple class, something like this:

public class Class1 {

    private String name;
    //...
    public String getName() {
         return this.name;
    }
}

And a wrapper class:

public class Class2 {

    private Class1 class1;

    public Class2(Class1 class1) {
        this.class1 = class1;
    }

    public String getName() {
        return class1.getName();
    }

    //...
}

And now a test case for the latter:

@RunWith(Jmock.class)
public class TestClass {

    	JUnit4Mockery context = new JUnit4Mockery() {
	    {
	         setImposteriser(ClassImposteriser.INSTANCE);
        }};

        final Class1 class1 = context.mock(Class1.class);

        @Test
        public void shouldReturnTheValueNameForGetName() {
            Class2 class2 = new Class2(class1);

            context.checking(new Expectations() {
                {
                    oneOf(class1).getName();
                    will(returnValue("name"));
                }
            }
            Assert.assertEquals("name", class2.getName());
        }
}

I mean why? Why would anyone put that value into an assert? It’s obvious that this assert will never fail. We told the mocking framework to return the value ‘name’ when the method getName() is called. Of course it returns the value we told it to return. So the assert is completely unnecessary there.

Of course, no one writes tests for getters… Don’t get me wrong, I’m not complaining about writing tests for getters. But still, people keep writing test cases that test values returned by the mocking framework. These tests can, at most, test the framework itself.

Anyway, values returned by expectations should never be tested. It’s like Assert.assertTrue(true); Tests that can’t fail are not testing anything. Tests that not test anything should be removed from test suites.

Advertisements

Author: tamasgyorfi

Senior software engineer, certified enterprise architect and certified Scrum master. Feel free to connect on Twitter: @tamasgyorfi

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