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:

iloveyouboss/test-3/test/util/SearchTest.java
 
Search search = ​new​ Search(stream, ​"practical joke"​, ​"1"​);

and:

iloveyouboss/test-3/test/util/SearchTest.java
 
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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.