import java.util.*; import static org.junit.Assert.*; import org.junit.*; public class DateTest { // private ... private Random rand = new Random(); // run this code before the unit tests @Before public void setup() { // ...db.. } @Test public void testDateIntIntInt1() { Date d = new Date(2008, 2, 15); assertEquals(2008, d.getYear()); assertEquals(2, d.getMonth()); assertEquals(15, d.getDay()); Date d2 = new Date(1979, 9, 19); assertEquals(1979, d2.getYear()); assertEquals(9, d2.getMonth()); assertEquals(19, d2.getDay()); } @Test(expected = IllegalArgumentException.class) public void testDateIntIntInt2() { new Date(2008, -2, 57); } @Test public void testAddDays() { // sep 1 1752 +2 => 14 :'( // 2038 => 2039 // add 1 day // add 0 days // add negative days // add enough days to make year 5-digits // test 3 leap year cases // every 4 unless 100 unless 400 // add 2 days from feb 28 in leap year addDaysHelper(2008, 2, 28, 2, 2008, 3, 1); // wrapping around months // garbage (strings) // call addDays TWICE // 1) split method up // 2) helper methods } private void addDaysHelper(int m, int d, int add, int m2, int d2) { addDaysHelper(2008, m, d, add, 2008, m2, d2); } private void addDaysHelper(int y, int m, int d, int add, int y2, int m2, int d2) { Date date = new Date(y, m, d); date.addDays(add); assertEquals(date.toString(), y2, date.getYear()); assertEquals(date.toString(), m2, date.getMonth()); assertEquals(date.toString(), d2, date.getDay()); } }