Line 1: | Line 1: | ||
The following toy example tests a few situations (some fail, to illustrate all cases). | The following toy example tests a few situations (some fail, to illustrate all cases). | ||
− | + | <java5> | |
import junit.framework.*; | import junit.framework.*; | ||
import java.util.ArrayList; | import java.util.ArrayList; | ||
Line 19: | Line 19: | ||
try { | try { | ||
ArrayList<Object> emptyList = new ArrayList<Object>(); | ArrayList<Object> emptyList = new ArrayList<Object>(); | ||
− | Object o = emptyList.get(0); | + | Object o = emptyList.get(0); // throws IndexOutOfBoundsException |
fail("no exception"); | fail("no exception"); | ||
} | } | ||
Line 36: | Line 36: | ||
} | } | ||
+ | </java5> |
The following toy example tests a few situations (some fail, to illustrate all cases). <java5>
import junit.framework.*; import java.util.ArrayList; public class HelloWorld3 extends TestCase { public void testMultiplication() { // Testing if 3*2=6: assertEquals("Multiplication", 6, 3*2); } public void testCreativeMultiplication() { // Testing if 3*2=6: assertEquals("Creative multiplication", 7, 3*2); } public void testException1() { try { ArrayList<Object> emptyList = new ArrayList<Object>(); Object o = emptyList.get(0); // throws IndexOutOfBoundsException fail("no exception"); } catch (Exception e) { } } public void testException2() { try { int i = 0; } catch (Exception e) { fail("exception"); } } }
</java5>