TCSS 360, Spring 2005 Lecture Notes - PowerPoint PPT Presentation

1 / 5
About This Presentation
Title:

TCSS 360, Spring 2005 Lecture Notes

Description:

Test Infected: Programmers Love Writing Tests. 2. JUnit and Eclipse ... testing methods (name of method MUST start with 'test' ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 6
Provided by: facultyWa
Category:
Tags: tcss | lecture | love | notes | spring | tests

less

Transcript and Presenter's Notes

Title: TCSS 360, Spring 2005 Lecture Notes


1
TCSS 360, Spring 2005Lecture 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)
  • uses , not .equals
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com