JUnit 4

Not least because it is so much simpler, but regular usage will result in this will becoming ingrained, (this is covered in greater detail here), but being as I’m so used to the previous incarnations of JUnit it might be worth noting the usage for quick look-up…

package junit4; 

import mypackage.ClassToTest; 
import org.junit.Before; 
import org.junit.After; 
import org.junit.Ignore; 
import org.junit.Test; 

import static org.junit.Assert.*; 

public class MyJUnit4Test { 
  private static ClassToTest classToTest = new ClassToTest(); 
  
  @Before 
  public void whatUsedToBeInSetup() { 
    // test set up type activities 
  } 
  
  @Test 
  public void first() { 
    classToTest.doWhatYouDo(); 
    assertEquals(classToTest.getWhatItDid(), result); 
  } 
  
  @Test(expected = SomeParticularException.class) 
  public void shouldCauseExceptionInTestClass() { 
    classToTest.doThatThingYouDontLike(); 
  } 
  
  @Ignore("not ready yet") 
  @Test 
  public void multiply() { 
    classToTest.doThatThingYouWillDo(); 
    assertEquals(classToTest.getWhatItDid(), futureResult); 
  } 
  
  @After 
  public void whatUsedToBeIntearDown() { 
    // test tear down type activities 
  } 

}

Minor Eclipse Annoyance - organise imports changes import static org.junit.Assert.* to whatever asserts you’ve actually referenced. This being an annoying when you’re developing iteratively and may not yet have used all the asserts you might wish to use.

comments powered by Disqus