Naming your unit tests
        Posted  
        
            by kerry
        on Gooder Code
        
        See other posts from Gooder Code
        
            or by kerry
        
        
        
        Published on Fri, 23 Jul 2010 15:00:40 +0000
        Indexed on 
            2010/12/06
            16:59 UTC
        
        
        Read the original article
        Hit count: 345
        
When you create a test for your class, what kind of naming convention do you use for the tests? How thorough are your tests? I have lately switched from the conventional camel case test names to lower case letters with underscores. I have found this increases the readability and causes me to write better tests.
A simple utility class:
public class ArrayUtils {
  public static < T > T[] gimmeASlice(T[] anArray, Integer start, Integer end) {
    // implementation (feeling lazy today)
  }
}
I have seen some people who would write a test like this:
public class ArrayUtilsTest {
  @Test
  public void testGimmeASliceMethod() {
    // do some tests
  }
}
A more thorough and readable test would be:
public class ArrayUtilsTest {
  @Test
  public void gimmeASlice_returns_appropriate_slice() {
    // ...
  }
  @Test
  public void gimmeASlice_throws_NullPointerException_when_passed_null() {
    // ...
  }
  @Test
  public void gimmeASlice_returns_end_of_array_when_slice_is_partly_out_of_bounds() {
   // ...
  }
  @Test
  public void gimmeASlice_returns_empty_array_when_slice_is_completely_out_of_bounds() {
    // ...
  }
}
Looking at this test, you have no doubt what the method is supposed to do. And, when one fails, you will know exactly what the issue is.
© Gooder Code or respective owner