Tuesday, August 21, 2007

JUnit Quick Start

JUnit 4 introduced some significant changes to the code and to how tests are executed. The comments in the Java code show how the code is compiled and executed.
import org.junit.*; 
import java.util.*;
/**  
 *    
 */
public class TestBooleanMapper {
    Map map = new HashMap();

    @Before
    public void setUp() {
        map.clear();
        map.put("x", new Boolean(false));
        map.put("y", new Boolean(true));
    }
    @Test
    public void testFalse() {
        Assert.assertFalse(map.get("x"));
    }
    @Test
    public void testTrue() {
        Assert.assertTrue(map.get("y"));
    }
    @Test
    public void testNull() {
        Boolean skip = map.get("z");
        Assert.assertFalse(skip==null ? false : skip);
    }
}


c:\src>javac -classpath .;junit-4.4.jar TestBooleanMapper.java
c:\src>java -classpath .;junit-4.4.jar org.junit.runner.JUnitCore TestBooleanMapper


 

import static org.junit.Assert.*; import org.junit.Test; public class CamelCaseTest { @Test public void testNowIsTheTime() { assertEquals("Now Is The Time For All Good People", CamelCase.camelCase("now is the time for all good people")); } }  

public class CamelCase { public static String camelCase(String original) { StringBuffer sb = new StringBuffer(); for (String subst : original.split(" ")) { sb.append( subst.substring(0,1).toUpperCase() + subst.substring(1).toLowerCase() + " "); } return sb.toString().trim(); } }  

C:\src\java\CamelCase>javac -cp .;junit-4.4.jar CamelCaseTest.java C:\src\java\CamelCase>java -cp .;junit-4.4.jar org.junit.runner.JUnitCore CamelCaseTest