Test Smell: Implicit Meaning and Misleading Organization
Explore how to identify test smells such as implicit meaning and misleading organization in JUnit tests. Learn to apply the Arrange-Act-Assert pattern and improve test data clarity to enhance understanding, speed up reading, and maintain test effectiveness.
We'll cover the following...
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());
}
}What should be inserted in lines 35 and 37?
Assert and Act respectively
Act and Assert respectively
Assert and Arrange respectively
Act and Arrange respectively
We’re getting close. Time for a final pass against the two tests!
Implicit meaning
The biggest question each of our ...