jeudi 1 octobre 2015

How to cover a void method that avoids null parameters by passing a null parameter?

I have a method like this:

public class SomeService {
    public void sortByFieldX(List<MyClass> list) {
        if (list != null) {
            Collections.sort(list, new ComparatorOfMyClassThatUsesFieldX());
        }
    }
}

How should I write a test for sortByFieldX where I pass null argument?

public class SomeServiceTest {
    @Test
    public void sortByFieldX() {
        List<MyClass> list = null;
        SomeService service = new SomeService();
        service.sortByFieldX(list);
        //assert what?
        //assertNull(list); is really stupid because Java is pass by value
    }
}

Is this test even valid? I'm just trying to write this test as part of a set of tests to cover all the branches in code.

Aucun commentaire:

Enregistrer un commentaire