Test Smell: Irrelevant Information

Learn how to eradicate irrelevant information with the use of meaningful constants.

We'll cover the following

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

Sometimes, we’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.

Magic literal and meaningful names

Magic literals are literals used arbitrarily, without a clear explanation of their meaning. Magic literals are considered poor practice because they reduce readability, decrease modularity, and encourage problematic code duplication.

Our test contains some magic literals in SearchTest that aren’t at all clear:

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

The other example is:

assertThat(search.getMatches(), containsMatches(new Match[] {
  new Match("1", "practical joke",
      "or a vast practical joke, though t") }));

Get hands-on with 1200+ tech skills courses.