title: Solid Java unit test automation? (JUnit/Hamcrest/...)
body: <h2>Intent</h2>
  
  <p>I am looking for the following:</p>
  
  <ul>
  <li>A solid unit testing methodology
  <ol>
  <li>What am I missing from my approach?</li>
  <li><strong>What am I doing wrong</strong>?</li>
  <li><em>What am I doing which is unnecessary?</em></li>
  </ol></li>
  <li>A way to get as much as possible done automatically</li>
  </ul>
  
  <h2>Current environment</h2>
  
  <ul>
  <li><strong><a href="http://eclipse.org/">Eclipse</a></strong> as IDE</li>
  <li><strong><a href="http://www.junit.org/">JUnit</a></strong> as a testing framework, integrated into Eclipse</li>
  <li><strong><a href="http://code.google.com/p/hamcrest/">Hamcrest</a></strong> as a "matchers" library, for better assertion readability</li>
  <li><strong><a href="http://code.google.com/p/guava-libraries/">Google Guava</a></strong> for precondition validation</li>
  </ul>
  
  <h2>Current approach</h2>
  
  <h3>Structure</h3>
  
  <ul>
  <li>One test class per class to test</li>
  <li>Method testing grouped in static nested classes</li>
  <li>Test method naming to specify behaviour tested + expected result</li>
  <li>Expected exceptions specified by Java <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Annotation.html">Annotation</a></strong>, not in method name</li>
  </ul>
  
  <h3>Methodology</h3>
  
  <ul>
  <li>Watch out for <code>null</code> values</li>
  <li>Watch out for empty <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/util/List.html">List&lt;E&gt;</a></strong></li>
  <li>Watch out for empty <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html">String</a></strong></li>
  <li>Watch out for empty arrays</li>
  <li>Watch out for object state invariants altered by code (post-conditions)</li>
  <li>Methods accept documented parameter types</li>
  <li>Boundary checks (e.g. <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#MAX_VALUE">Integer.MAX_VALUE</a></strong>, etc...)</li>
  <li>Documenting immutability through specific types (e.g. <strong>Google Guava <a href="http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableList.html">ImmutableList&lt;E&gt;</a></strong>)</li>
  <li><em>... is there a list for this? Examples of nice-to-have testing lists:</em>
  <ul>
  <li>Things to check in database projects (e.g. CRUD, connectivity, logging, ...)</li>
  <li>Things to check in multithreaded code</li>
  <li>Things to check for EJBs</li>
  <li>... ?</li>
  </ul></li>
  </ul>
  
  <h2>Sample code</h2>
  
  <p><em>This is a contrived example to show some techniques.</em></p>
  
  <hr>
  
  <h3>MyPath.java</h3>
  
  <pre><code>import static com.google.common.base.Preconditions.checkArgument;
  import static com.google.common.base.Preconditions.checkNotNull;
  import java.util.Arrays;
  import com.google.common.collect.ImmutableList;
  public class MyPath {
    public static final MyPath ROOT = MyPath.ofComponents("ROOT");
    public static final String SEPARATOR = "/";
    public static MyPath ofComponents(String... components) {
      checkNotNull(components);
      checkArgument(components.length &gt; 0);
      checkArgument(!Arrays.asList(components).contains(""));
      return new MyPath(components);
    }
    private final ImmutableList&lt;String&gt; components;
    private MyPath(String[] components) {
      this.components = ImmutableList.copyOf(components);
    }
    public ImmutableList&lt;String&gt; getComponents() {
      return components;
    }
    @Override
    public String toString() {
      StringBuilder stringBuilder = new StringBuilder();
      for (String pathComponent : components) {
        stringBuilder.append("/" + pathComponent);
      }
      return stringBuilder.toString();
    }
  }
  </code></pre>
  
  <hr>
  
  <h3>MyPathTests.java</h3>
  
  <pre><code>import static org.hamcrest.Matchers.is;
  import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
  import static org.hamcrest.collection.IsEmptyCollection.empty;
  import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
  import static org.hamcrest.core.IsEqual.equalTo;
  import static org.hamcrest.core.IsNot.not;
  import static org.hamcrest.core.IsNull.notNullValue;
  import static org.junit.Assert.assertThat;
  import org.junit.Test;
  import org.junit.experimental.runners.Enclosed;
  import org.junit.runner.RunWith;
  import com.google.common.base.Joiner;
  @RunWith(Enclosed.class)
  public class MyPathTests {
    public static class GetComponents {
      @Test
      public void componentsCorrespondToFactoryArguments() {
        String[] components = { "Test1", "Test2", "Test3" };
        MyPath myPath = MyPath.ofComponents(components);
        assertThat(myPath.getComponents(), contains(components));
      }
    }
    public static class OfComponents {
      @Test
      public void acceptsArrayOfComponents() {
        MyPath.ofComponents("Test1", "Test2", "Test3");
      }
      @Test
      public void acceptsSingleComponent() {
        MyPath.ofComponents("Test1");
      }
      @Test(expected = IllegalArgumentException.class)
      public void emptyStringVarArgsThrows() {
        MyPath.ofComponents(new String[] { });
      }
      @Test(expected = NullPointerException.class)
      public void nullStringVarArgsThrows() {
        MyPath.ofComponents((String[]) null);
      }
      @Test(expected = IllegalArgumentException.class)
      public void rejectsInterspersedEmptyComponents() {
        MyPath.ofComponents("Test1", "", "Test2");
      }
      @Test(expected = IllegalArgumentException.class)
      public void rejectsSingleEmptyComponent() {
        MyPath.ofComponents("");
      }
      @Test
      public void returnsNotNullValue() {
        assertThat(MyPath.ofComponents("Test"), is(notNullValue()));
      }
    }
    public static class Root {
      @Test
      public void hasComponents() {
        assertThat(MyPath.ROOT.getComponents(), is(not(empty())));
      }
      @Test
      public void hasExactlyOneComponent() {
        assertThat(MyPath.ROOT.getComponents(), hasSize(1));
      }
      @Test
      public void hasExactlyOneInboxComponent() {
        assertThat(MyPath.ROOT.getComponents(), contains("ROOT"));
      }
      @Test
      public void isNotNull() {
        assertThat(MyPath.ROOT, is(notNullValue()));
      }
      @Test
      public void toStringIsSlashSeparatedAbsolutePathToInbox() {
        assertThat(MyPath.ROOT.toString(), is(equalTo("/ROOT")));
      }
    }
    public static class ToString {
      @Test
      public void toStringIsSlashSeparatedPathOfComponents() {
        String[] components = { "Test1", "Test2", "Test3" };
        String expectedPath =
            MyPath.SEPARATOR + Joiner.on(MyPath.SEPARATOR).join(components);
        assertThat(MyPath.ofComponents(components).toString(),
            is(equalTo(expectedPath)));
      }
    }
    @Test
    public void testPathCreationFromComponents() {
      String[] pathComponentArguments = new String[] { "One", "Two", "Three" };
      MyPath myPath = MyPath.ofComponents(pathComponentArguments);
      assertThat(myPath.getComponents(), contains(pathComponentArguments));
    }
  }
  </code></pre>
  
  <hr>
  
  <h2>Question, phrased explicitly</h2>
  
  <ul>
  <li><p>Is there a list of techniques to use to build a unit test? Something much more advanced than my oversimplified list above (e.g. check nulls, check boundaries, check expected exceptions, etc.) perhaps available in a book to buy or a URL to visit?</p></li>
  <li><p>Once I have a method that takes a certain type of parameters, can I get any <strong><a href="http://eclipse.org/">Eclipse</a></strong> plug-in to generate a stub for my tests for me? Perhaps using a Java <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Annotation.html">Annotation</a></strong> to specify metadata about the method and having the tool materialise the associated checks for me? (e.g. @MustBeLowerCase, @ShouldBeOfSize(n=3), ...)</p></li>
  </ul>
  
  <p>I find it tedious and robot-like to have to remember all of these "QA tricks" and/or apply them, I find it error-prone to copy and paste and I find it not self-documenting when I code things as I do above. Admittedly, Hamcrest <a href="http://code.google.com/p/hamcrest/wiki/MatcherLibraries">libraries</a> go in the general direction of specialising types of tests (e.g. on <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html">String</a></strong> objects using RegEx, on <strong><a href="http://docs.oracle.com/javase/7/docs/api/java/io/File.html">File</a></strong> objects, etc) but obviously do not auto-generate any test stubs and do not reflect on the code and its properties and prepare a harness for me.</p>
  
  <p>Help me make this better, please.</p>
  
  <h2>PS</h2>
  
  <p><em>Do not tell me that I am just presenting code which is a silly wrapper around the concept of creating a Path from a list of path steps supplied in a static factory method please, this is a totally made-up example but it shows a "few" cases of argument validation... If I included a much longer example, who would really read this post?</em></p>