Is the parameter used?

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.

Resources



(less...)

Pages

About
Events
Why

Categories

Agile
Automation
BDD
Clean code
Continuous delivery
Continuous deployment
Continuous integration
Cucumber
Culture
Design
DevOps
Executable specification
Git
Gradle
Guice
J2EE
JUnit
Java
Javascript
Kubernetes
Linux
Load testing
Maven
Mockito
New developers
Pair programming
PicoContainer
Presentation
Programming
Public speaking
Quality
React
Recruiting
Requirements
Scala
Selenium
Software craftsmanship
Software development
Spring
TDD
Teaching
Technical debt
Test automation
Tools
Web
Windows
eXtreme Programming

Authors

Thomas Sundberg
Adrian Bolboaca

Archives

Meta

rss RSS