Thomas Sundberg

July 31, 2013

Is the parameter used?

Filed under: Java, Programming — Tags: , , , — Thomas Sundberg @ 19:57

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.

Resources

About these ads

2 Comments »

  1. Tracer bullets were described in The Pragmatic Programmer book by Andy Hunt and Dave Thomas. You can also read about it here: http://www.artima.com/intv/tracer.html

    What you’re describing here doesn’t sound related at all.

    Comment by Aslak Helles√ły — July 31, 2013 @ 21:18

    • Hi!

      You are quite right Aslak. I rewrote it a bit.

      Thanks for the pointer.

      /Thomas

      Comment by Thomas Sundberg — August 1, 2013 @ 02:54


RSS feed for comments on this post. TrackBack URI

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

Customized Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 49 other followers

%d bloggers like this: