println it

Software blog about tools, builds and making it all work

JUnit Updates

After talking about TDD there comes time to bring my JUnit knowledge up-to-speed with 4.x and 4.8.x versions. Also, I see version 4.8.2 has finally made its way to central Maven repo.

“JUnit Kung Fu: Getting More Out of Your Unit Tests” (audio) teaches some new JUnit tricks.

  • Normally we call the class TestSomething and methods testFunctionalityZ(). Well, it’s kind of .. boring, isn’t it? Let’s give some respect to names! (slide 10)
    public class WhenSomethingHappens
    {
        @Test
        public void somethingShouldBeThis(){ .. }
    
        @Test
        public void somethingElseShouldBeThat(){ .. }
    
        @Test
        public void thisShouldNeverHappen(){ .. }
    }
    

    By replacing 'test' with 'should' we emphasize that we test behavior and not implementation.

  • The structure of each method should normally follow Arrange-Act-Assert structure (slide 11): prepare the environment, do what needs to be done, check results.
  • Hamcrest assertions! (slide 13) Part of JUnit already.
    assertThat( theBiscuit, is( myBiscuit ));
    assertThat( x, is( not( 4 )));
    assertThat( myList, hasItem( "3" ));
    
  • Parametrized tests (slide 27).
    @RunWith ( Parameterized.class )
    public class WhenSomethingHappens
    {
        private int x;
        private int y;
    
        public WhenSomethingHappens ( int x, int y )
        {
            this.x = x;
            this.y = y;
        }
    
        @Parameters
        public static Collection<Object[]> testData() 
        { 
            return Array.asList( new Object[][]{{ 1, 2 }, 
                                                { 3, 4 }, 
                                                { 5, 6 }, .. });
        }
    
        @Test
        public void thisShouldBeThat () { assert( this.x, is( this.y - 1 )) }
    }
    

    Static method marked with @Parameters provides “bulks of data” and test instance will be initialized for each element in the Collection returned. This way the test method is absolutely free from any interfering loops and iterations over various values, which can be now conveniently loaded from external resources like Excel files.

  • JUnit @Rules (slide 35) – AOP-like test interceptors, providing additional functionalities like temporary test folder, test errors collector, test timeout timer, etc.
    public class WhenSomethingHappens
    {
        @Rule
        public TemporaryFolder folder = new TemporaryFolder();
     
        @Test
        public void thisShouldBeThat()
        {
            File createdFile   = folder.newFile( "1.txt" );
            File createdFolder = folder.newFolder( "someFolder" );
            ...
        }
     }
     
  • Parallel tests (slide 48) when you run on multiply CPUs.
  • Infinitest! (slide 50) Continuous testing in Eclipse and IDEA whenever sources are saved. When some test breaks one becomes aware of that immediately and not 2 hours later.


Some more JUnit-related resources:

, ,

One Response to “JUnit Updates”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>