JUnit 4 Test Class with annotations

Tuesday, June 8th, 2010

A simple example of a JUnit 4 Test class marked up with annotations.

import org.junit.*; 
import static org.junit.Assert.*; 

public class SampleTest { 
    private java.util.List emptyList; 

    /** 
     * Sets up the test fixture. 
     * (Called before every test case method.) 
     */ 
    @Before 
    public void setUp() { 
        emptyList = new java.util.ArrayList(); 
    } 

    /** 
     * Tears down the test fixture. 
     * (Called after every test case method.) 
     */ 
    @After 
    public void tearDown() { 
        emptyList = null; 
    } 

    @Test 
    public void testSomeBehavior() { 
        assertEquals("Empty list should have 0 elements", 0, emptyList.size()); 
    } 

    @Test(expected=IndexOutOfBoundsException.class) 
    public void testForException() { 
        Object o = emptyList.get(0); 
    } 
}

Points of note

The static import of org.junit.Assert.*

You can leave a response, or trackback from your own site.

Tags: , ,
Posted in: Examples



Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>