Com S 362 --- Object-Oriented Analysis and Design EXERCISE 14: JUNIT-BASED UNIT TESTING (File $Date: 2004/11/17 04:23:32 $) The purpose of this exercise is for you to get some experience with JUnit and unit testing. As with all exercises, this is to be done individually, not in teams. And it is due the day this topic is planned to be discussed in class, unless specified otherwise (see the syllabus at: http://www.cs.iastate.edu/~cs362/syllabus.shtml). As with all exercises, you have two choices for doing the work. You can either: - complete it as specified or - write down questions or problems that you had in trying to complete it. If you write down questions or problems you have, these should be detailed enough so that we can tell that you have read the materials and thought about them. (Don't just write: "I couldn't get it to work; say what you tried and what you didn't understand.) During the class where this exercise is discussed, you should try to get help with these by explaining what you did and what your problems or confusions are. Don't be shy; there will be other people with the same problem, and everyone can learn by discussing these issues. 1. [JUnit testing] Read the article "Test Infected: Programmers Love Writing Tests.", by Kent Beck and Erich Gamma (which appeared in the Java Report, volume 3, number 7, 37-50, 1998), and is online at: http://junit.sourceforge.net/doc/testinfected/testing.htm Also, this problem uses JML. For an introduction to JML, see the paper "Design by contract with JML", by Gary T. Leavens and Yoonsik Cheon. It is available from http://jmlspecs.org or directly from: ftp://ftp.cs.iastate.edu/pub/leavens/JML/jmldbc.pdf Other documentation on JML is available at http://jmlspecs.org. For this problem first make a copy, in your own directory, of the file HistoricalData.java which is in /home/course/cs362/public/exercises/ex14/ on the department Linux machines. For example: $ cp /home/course/cs362/public/exercises/ex14/HistoricalData.java . This file is also available from the course web page. Note that the file has correct javadoc comments, correct JML specifications, and incorrect code. While the comments indicate how to fix the code, you should *not* change the code of HistoricalData. Instead this problem is about writing a test to find these problems. Write a JUnit test class, named HistoricalDataTest, which does unit testing on the class HistoricalData. (Hint: you can use Eclipse to generate a skeleton test file that has a main method with the "text ui" interface to get started on this.) You should write a test that has a test method for each method of HistoricalDataTest. (You don't have to write a separate test for HistoricalData's constructor or for the toString method if you don't want to. Note that a subclass of TestCase should have a public constructor as follows public HistoricalDataTest(String name) { super(name); } that takes a single string as an argument, and calls the constructor of TestCase with that string.) Run your test with the JUnit textual interface on the supplied version of HistoricalDataTest. Ideally, it should find failures on all the methods. Hand in a printout of your HistoricalDataTest class and the output from this test run. You can get output from the JUnit tests from within Eclipse by running the HistoricalDataTest as a Java application instead of as a JUnit test, and copying the output from the Eclipse console window. Or you can do this outside of Eclipse, in the Unix shell, by using the command line (at the Unix prompt): $ java HistoricalDataTest > HistoricalDataTest.out This also should work on Windows (in a console) or a Mac. All of these require that the junit.jar file be in your CLASSPATH (the Java Build Path in Eclipse). WHAT TO HAND IN You should have at the beginning of class, written answers to the above questions (or written out questions and problems you encountered for each part). Make sure your name is on these. Attach the printouts, if any, requested above. ADDITIONAL READINGS If you have time, read about how to combine JML and JUnit testing on the JML web site.