Test Smell: Irrelevant Information

A well-abstracted test emphasizes everything that’s important to understanding it and deemphasizes anything that’s not. The data used in a test should help tell a story.

Sometimes you’re forced to supply data to get code to compile, even though that data is irrelevant to the test at hand. For example, a method might take additional arguments that have no impact on the test.

Our test contains some “magic literals” that aren’t at all clear:

Search search = ​new​ Search(stream, ​"practical joke"​, ​"1"​);


assertThat(search.getMatches(), containsMatches(​new​ Match​[]​ {
new​ Match(​"1"​, ​"practical joke"​,

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.