The Compulsive Coder, Episode 7: Please AAA

by Jeff Langr

November 15, 2016

Q. What behavior does the following JUnit test describe? (In other words, what’s an appropriate name for the test?)

            MockInitialContext mic = MockInitialContextFactory.getContext();
            mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
    
            LoggerFactory.getLogger(ContextDetachingSCLTest.class);
    
            ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
            Context context = selector.getLoggerContext();
            assertEquals("tata", context.getName());
            System.out.println(selector.getContextNames());
            assertEquals(2, selector.getCount());

Maybe it helps, just a bit, if I tell you that the test name is testCreateContext.

Why must I spend time reading through each line of a test like this to understand what it’s really trying to prove?

The test designer might have made the central point of the test obvious, by visually chunking the test into its Arrange-Act-Assert (AAA) parts:

            MockInitialContext mic = MockInitialContextFactory.getContext();
            mic.map.put(ClassicConstants.JNDI_CONTEXT_NAME, "tata");
            LoggerFactory.getLogger(ContextDetachingSCLTest.class);
            ContextJNDISelector selector = (ContextJNDISelector) ContextSelectorStaticBinder.getSingleton().getContextSelector();
    
            Context context = selector.getLoggerContext();
    
            assertEquals("tata", context.getName());
            System.out.println(selector.getContextNames());
            assertEquals(2, selector.getCount());

Well, I think that’s right at least, given what we know.

(Yes, the word “test” in the name duplicates the requisite @Test annotation. And yes, there are other problems with the test, e.g. printing anything to the console. But it’s excruciatingly simple to first ensure the structure of a test is clear.)

Standards are only that if they are constantly adhered to.

Pingback: The Compulsive Coder, Episode 1: The Stub Comment

Pingback: The Compulsive Coder, Episode 2: Syntax Coloring

Pingback: The Compulsive Coder, Episode 3: Typing Isn’t the Bottleneck

Pingback: The Compulsive Coder, Episode 4: You Create, It Assigns

Pingback: The Compulsive Coder, Episode 5: Extract Method Flow

Pingback: The Compulsive Coder, Episode 6: this Duplication is Killing Me

Pingback: The Compulsive Coder, Episode 7: Please AAA

Pingback: The Compulsive Coder, Episode 8: You Might Not Like This

Share your comment

Jeff Langr

About the Author

Jeff Langr has been building software for 40 years and writing about it heavily for 20. You can find out more about Jeff, learn from the many helpful articles and books he's written, or read one of his 1000+ combined blog (including Agile in a Flash) and public posts.