How do I write and run a simple JUnit test?
• Create a class: package junitfaq; import org.junit.*; import static org.junit.Assert.*; import java.util.*; public class SimpleTest { • Write a test method (annotated with @Test) that asserts expected results on the object under test: @Test public void testEmptyCollection() { Collection collection = new ArrayList(); assertTrue(collection.isEmpty()); } • If you are running your JUnit 4 tests with a JUnit 3.x runner, write a suite() method that uses the JUnit4TestAdapter class to create a suite containing all of your test methods: public static junit.framework.Test suite() { return new junit.framework.JUnit4TestAdapter(SimpleTest.class); } • Although writing a main() method to run the test is much less important with the advent of IDE runners, it’s still possible: public static void main(String args[]) { org.junit.runner.JUnitCore.main(“junitfaq.SimpleTest”); } } • Run the test: • To run the test from the console, type: java org.junit.runner.JUnitCore junitfaq.SimpleTest • To run the t