Filed under: Java, — Tags: JUnit — Thomas Sundberg — 2013-07-31
I watched a presentation by Zsolt Fabok a while ago and saw a technique I hadn't thought of before. How do you verify that a parameter is used in a simple way? Feed the method with something that usually doesn't work and verify that something happens.
Send in null and assert that you actually receive a NullPointerException like this:
@Test(expected = NullPointerException.class)
public void shouldVerifyParameterUsage() {
Example example = new Example();
example.foo(null);
}
There are other ways to trace a parameter or verify that it is being used, but this is a trivial way that often works. Similar to printf debugging. It is not pretty, but it is always available and works.