Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 360, Spring 2005 Lecture Notes

Similar presentations


Presentation on theme: "TCSS 360, Spring 2005 Lecture Notes"— Presentation transcript:

1 TCSS 360, Spring 2005 Lecture Notes
JUnit Testing Relevant Reading: Test Infected: Programmers Love Writing Tests

2 JUnit and Eclipse Adding JUnit to your Eclipse project:
click Project -> Properties -> Add External JARs... -> eclipse folder/plugins/org.junit_3.8.1/junit.jar Create the test case junit.framework.TestCase implements Test click File -> New -> JUnit Test Case

3 JUnit TestCase methods
public void setUp() run at start of each test public void tearDown() run at end of each test public YourClassName () // constructor run once at start of ALL tests (when testing object is created)

4 JUnit TestCase assertions
testing methods (name of method MUST start with 'test') Formatting key: optional can be any type (primitive or Object) public void assertEquals(String message, Object expected, Object actual) note: no Assert. required public void assertFalse(String message, boolean condition) public void assertNotEquals(String message, Object expected, Object actual) public void assertNotNull(String message, Object obj) public void assertNotSame(String message, Object expected, Object actual) uses ==, not .equals public void assertSame(String message, Object expected, Object actual) public void assertTrue(String message, boolean condition) public void fail(String message)

5 JUnit exercise Using our Date class, let's:
Enforce a contract with invariants on our Date objects, so that they can never hold an invalid state. Write an addDays method that takes an int and adjusts the current Date's date by the given number of days forward in time. If the argument is negative, go backward in time. Write a compareTo method that compares Dates chronologically. Write an equals method that tells whether two Dates store the same date. Write getDaysFrom that takes another Date object and returns the number of days this Date is away from the date of the given other Date object. (It is therefore implied that after a call of this.addDays(-this.getDaysFrom(other)), the statement this.equals(other) would be true.) Write JUnit test code that checks each of the above methods for correctness. Utilize intelligent testing techniques as presented in class. a good equals method has some initial checks like checking for null, checking whether this == that, check instanceof, etc... then it calls compareTo


Download ppt "TCSS 360, Spring 2005 Lecture Notes"

Similar presentations


Ads by Google