...

/

Test Smell: Implicit Meaning and Misleading Organization

Test Smell: Implicit Meaning and Misleading Organization

Learn the way developers should use the principles of Arrange, Act, and Assert in writing tests.

Misleading organization

Knowing which part of the test is the act part, which is the arrange part, and which is the assert can speed up understanding. With this in mind, we can use AAA to make the test’s intent explicit. The highlighted lines in the following listing show the blank lines to insert (act or assert) for each test:

package util;

// misleading organization
import java.io.*;
import java.net.*;
import java.util.logging.*;
import org.junit.*;
import static org.junit.Assert.*;
import static util.ContainsMatches.*;

public class SearchTest {
   private static final String A_TITLE = "1";
   private InputStream stream;
   
   @Before
   public void turnOffLogging() {
      Search.LOGGER.setLevel(Level.OFF);
   }
   
   @After
   public void closeResources() throws IOException {
      stream.close();
   }

   @Test
   public void returnsMatchesShowingContextWhenSearchStringInContent() {
      stream = streamOn("There are certain queer times and occasions "
            + "in this strange mixed affair we call life when a man "
            + "takes this whole universe for a vast practical joke, "
            + "though the wit thereof he but dimly discerns, and more "
            + "than suspects that the joke is at nobody's expense but "
            + "his own.");
      Search search = new Search(stream, "practical joke", A_TITLE);
      search.setSurroundingCharacterCount(10);

      search.execute();

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

   @Test
   public void noMatchesReturnedWhenSearchStringNotInContent() 
         throws MalformedURLException, IOException {
      URLConnection connection = 
            new URL("http://bit.ly/15sYPA7").openConnection();
      stream = connection.getInputStream();
      Search search = new Search(stream, "smelt", A_TITLE);

      search.execute();

      assertTrue(search.getMatches().isEmpty());
   }

   private InputStream streamOn(String pageContent) {
      return new ByteArrayInputStream(pageContent.getBytes());
   }
}
SearchTest.java
1

What should be inserted in lines 35 and 37?

A)

Assert and Act respectively

B)

Act and Assert respectively

C)

Assert and Arrange respectively

D)

Act and Arrange respectively

Question 1 of 20 attempted

We’re getting close. Time for a final pass against the two tests!

Implicit meaning

The biggest question each of our tests ...