(New page: The following toy example tests a few situations (some fail, to illustrate all cases). import junit.framework.*; import java.util.ArrayList; public class HelloWorld3 extends TestCase...) |
|||
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); <font color='red'>// throws IndexOutOfBoundsException</font> |
fail("no exception"); | fail("no exception"); | ||
} | } |
The following toy example tests a few situations (some fail, to illustrate all cases).
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 testWrongMultiplication() { // Testing if 3*2=6: assertEquals("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"); } } }