This action might not be possible to undo. Are you sure you want to continue?
Justin Pearson Palle Raabjerg Farshid Hassani Bijarbooneh Deadline: 30th of November
1. You are expected to work in pairs. 2. To pass the Lab you need to complete both Tutorial Exercises and the Assignment Tasks. 3. You can complete the Tutorial during the Lab and present it to the instructor, but if for some good reasons you can’t attend the lab then you can book a time with either Farshid or Palle to present and discuss the Tutorial exercises. 4. Once you are done with the Tutorial exercises you should complete the tasks in the assignment section of the lab, all you need to do is to follow the ToDo Checklist at the end of this assignment. 5. Archive your project folder in zip format and name it FirstName1.LastNameFirstName2.LastName2.zip where the ﬁrst name and last name refers to your name and your partner name participating in the assignment. 6. Login to the course manager and submit your zip archive ﬁle before the deadline.
In this assignment, you will work with the JUnit framework to perform Java unit testing. Junit is an open source project available at www.junit.org which is already integrated into Eclipse 1 3.4 and later. We will be working with JUnit version 4. Both Eclipse and JUnit should be installed and available on the Solaris terminals. This Lab consists of two parts. In the ﬁrst part you will learn a bit about JUnit by doing a few tutorial-like mini-exercises. In the second section you work on a set of tasks with the classes Currency, Money and Bank.
Eclipse is an open source IDE (integrated development enviroment) for Java, available from www.eclipse.org
java where classname is the name of the test class that should always end with Test. • assertSame(expected object. generally these units are classes and their methods. Your test class includes a test method for every method implemented by the class being tested. actual object): Asserts that two objects refer to the same object. string. Using JUnit you can write the test code according to the speciﬁcation of the methods and functions.Assignment 1 Testing Methodology What is JUnit? JUnit is a framework for testing units of your code. What you want to do is to make sure that the methods in your classes output/generate the expected results. the code is functioning as deﬁned by the speciﬁcation. If the expected and actual values are double or ﬂoat we need a third parameter called delta. delta): Assert that expected value is equal to the actual value. functions or procedures. There are many types of assertions available in JUnit. byte. Basic Information Before you begin. JUnit uses assertions to check if the actual result of a function matches the expected value. • Test Case Method: The methods you create to test the functionality of the methods in your main code. char or any Java object. These software development methodologies are based on writing the test code even before writing the methods to be tested. This can be a great help in making sure that your code stays robust and relatively bug-free. note that in JUnit we have the following deﬁnitions: • Test Case Class: This is a Java class where you create your unit tests. Following this procedure. Unit testing is very common when you are doing test driven development (TDD) and extreme programming (XP). But it is still a good convention to follow. It represents the maximum diﬀerence between expected and actual value for which both numbers are still considered equal. double. you will end up writing a set of test cases in the beginning and later ﬁll in the body of your methods and functions in a way that matches the speciﬁcation and passes the test cases. It is named [classname]Test. • assertFalse(condition): Asserts that the Boolean condition is False. in JUnit 4. • assertTrue(condition): Asserts that the Boolean condition is True. These tests should be written to make it likely that when all the tests pass. 2 . The expected and actual value can be of any type. In unit testing you write one test class for each of the classes that you want to test. These methods are annotated with @Test. actual. • assertNull(object): Asserts that an object is null. for example integer. • assertNotNull(object): Asserts that an object is not null. Here are some of the most commonly used assertions: • assertEquals(expected. Unlike JUnit 3.x the method name is not required to begin with test .
it will throw an AssertionError with a message showing which number it failed to generate. actual object): Asserts that two objects do not refer to the same object. choose File -> New -> Java Project.3. Eclipse has a wizard which will create these corresponding test methods for you.2. and the JUnit framework will show the infamous red bar of a failed test.1. If you ever wish to create test classes.13 by writing the expected value and calling the fib method to generate the number. otherwise you will have to mess with setting up the classpath and source libraries yourself).zip from Link. It is important 2 To read about Fibonacci numbers see http://en. which is caught by the JUnit framework and presented as a red bar.classpath and . Whenever an assertion fails. choose File -> Import -> Archive File.5. and there should be a number of errors showing up from the b_Money class in src/.org/wiki/Fibonacci number 3 .1. This is supposed to happen. The unit test classes are to be found in test/a_Introductory/ and the classes to be tested are in src/a_Introductory. In Eclipse. Some of the above assert statements accept an extra message parameter. and if Eclipse complains about it during the tutorial exercises. we take a look at the Fibonacci class.zip. To load Lab1. Browse for Lab1. Give it a name (“Lab1”. indicating test failure. You should now have a project with a test/ and a src/ directory. Fibonacci As the ﬁrst mini-exercise. and a red bar if any of them fail.project ﬁles (this is important.wikipedia.Assignment 1 Testing Methodology • assertNotSame(expected object. If the fib fails at generating the right number.zip into this project. Loading the Project Download the ﬁle Lab1. there is a testFib method corresponding to the fib method in the Fibonacci class. an AssertionError is thrown.java. Tutorial Exercises The package a_Introductory contains classes in both the test/ and the src/ library.8. for instance) and click Finish. This class is an attempt at implementing the recursive method ﬁb. This parameter is a String which will be displayed if the assertion fails. you may ignore it until you get to the Money part of the Lab. click Finish and answer Yes to letting it overwrite the . which should generate the nth Fibonacci Number2 . Notice that in FibonacciTest. Running the Test Cases When you run a test class JUnit will run each method annotated with @Test separately and show a green bar if all of them pass. In the test class we assert that the ﬁrst 7 Fibonacci numbers must be equal to 0.
It creates two rectangles with diﬀerent points and checks to see if the area and diagonal matches the expected values. you can annotate a method with @After.java and Money.java and read the source code... but we will come back to that in the following section. Note with satisfaction the green bar and move on to the Rectangle exercise. ﬁnd the bug in Fibonacci. and then ﬁll in the methods with code that will make your test cases pass. often called tearDown() if something needs to be cleaned up after each test case. To run the test.Assignment 1 Testing Methodology that anything happening in a test method is independent from the other test methods. and a speciﬁcation in the form of a JavaDoc document Link. Now. we have not written any test cases for it. 4 . JUnit will run each test method separately but uses the same test ﬁxture for all of them. 2. write test cases for each method of these classes. First. otherwise you risk getting weird results. Do the following: 1. select the FibonacciTest.) Assignment Tasks Money and Currency You have been given a template for the Currency and Money classes. Examine which test cases failed and check the failure messages. Do the following: 1. Observe the method setUp(). There is also a Bank class. This set of objects are called a test ﬁxture.java are empty.java. Likewise. which is annotated with @Before. that since the Point class is so blindingly simple. This method will be executed before each test case. 2. In this mini-exercise we test two methods from the Rectangle class. All the methods of Currency. Notice the new JUnit window appears and shows a red bar indicating that the test failed. You often set up a ﬁxture once and use the created objects several times in each test method. correct it and run the test again. See if you can ﬁnd it. Read the comments in all the Test Classes to ﬁnd out what is expected to be covered in the test case and what should be implemented. But in this example we have only one test method.java in the project explorer and choose Run -> Run As -> JUnit Test. Rectangle The Rectangle class deﬁnes a rectangle by holding two points and has two methods to calculate the area of the rectangle and the length of its diagonal. Run it as a JUnit test to see the red bar of a failed test. Below the listing of the running test cases you can see a stack of all failed test cases and their messages. Is the information about the error enough to spot the bug? can you improve the code to make it easier to spot this bug? (You should note by the way. There is a bug in the code. These two methods are using the same set of objects. Open RectangleTest.
2. Motivate your test cases by commenting in the test methods. Fix the bug and verify that BankTest. 5. Explain how you managed to ﬁnd the bug by running the unit tests. Find the bug in Bank. Again. Complete the methods body in Money. by commenting in the corresponding test methods. and specify it by commenting the code where the bug is spotted. Specify which one of your test cases fails.java classes.Assignment 1 Testing Methodology Bear in mind that the teaching assistants have their own test cases for these methods. 7.java passes successfully.java base on the JUnit failure from the last step.java and CurrencyTest. 6.java. Again make sure you follow the speciﬁcation for each method and implement the required speciﬁcation.java at this point. and comment where necessary. By reading the Bank.java and CurrencyTest.java speciﬁcation in JavaDoc make your test cases according to the comments in the BankTest. 5 . 4. If you run your test cases for MoneyTest. the speciﬁcation is provided in the JavaDoc document Link. 3. ToDo Checklist 1. they should be all passed. Bank The Bank class was written by a braindead programmer. When you are conﬁdent your Money and Currency classes work as intended.java and Currency. Make sure you read the available comments in the test methods and create your test cases according to those guidelines. Write the code for the body of the test cases methods for MoneyTest.java ﬁle. write test cases for the Bank class and ﬁnd the bugs. so write some good tests yourself.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue listening from where you left off, or restart the preview.