18
Procedure to create a test class using JUnit 3 using Eclipse Class definition to test: public class Student { private String name; private char gender; public String getName(){ return name; } public void setName(String name){ this.name = name; } public char getGender(){ return gender; } public void setGender(char gender){ this.gender = gender; } public Student(){ name = "Unknown"; gender = 'x'; } public Student(String name, char gender){ this.name = name; this.gender = gender; } public String toString(){ if (gender == 'm'){ return ("Name " + name + " - male"); } else if (gender == 'f'){ return ("Name " + name + " - female"); } else return ("Name and gender unknown"); } } On my Eclipse system, I saved this source code in package myPackage. The steps to create a JUnit test suite are given below: 1. Create a test file in the same project. Press File -> new -> source folder will ask for a source folder name. Give a name (I used testJUnit) and press “Finish”.

Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

  • Upload
    others

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

Procedure to create a test class using JUnit 3 using Eclipse Class definition to test: public class Student { private String name; private char gender; public String getName(){ return name; } public void setName(String name){ this.name = name; } public char getGender(){ return gender; } public void setGender(char gender){ this.gender = gender; } public Student(){ name = "Unknown"; gender = 'x'; } public Student(String name, char gender){ this.name = name; this.gender = gender; } public String toString(){ if (gender == 'm'){ return ("Name " + name + " - male"); } else if (gender == 'f'){ return ("Name " + name + " - female"); } else return ("Name and gender unknown"); } } On my Eclipse system, I saved this source code in package myPackage. The steps to create a JUnit test suite are given below:

1. Create a test file in the same project. Press File -> new -> source folder will ask for a source folder name. Give a name (I used testJUnit) and

press “Finish”.

Page 2: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

This folder will contain your test routines. It is advisable to separate the test classes from your classes to test.

2. Make sure your test classes are in the same package as to classes to test. Press File -> new -> Package. Select your source folder for the testing classes (testJUnit in my case). Put the same package name used in the class to test ( myPackage in this case) and press Finish. The appearance of the package explorer (the left part of the Eclipse window is shown below).

Page 3: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

3. Create the class name of the testing class. Convention is the name of the class you are testing followed by “Test”. So here we use “StudentTest” as the name of the class. Press File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class. c) Select the setup() method. This means you are going to specify the initial set up (i.e., which

objects you will create) when you are testing. d) The class you are testing is Student in this case e) Press next f) Select the names of the method you wish to test. I selected all the methods of Student,

except the constructor and pressed Finish. g) The system says JUnit 3 is not on the build path (See below). So add it to the build path.

Page 4: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

At this stage you should have a class StudentTest.java that looks as follows. package myPackage; import junit.framework.TestCase; public class StudentTest extends TestCase {

protected void setUp() throws Exception {

super.setUp(); } public void testGetName() { fail("Not yet implemented"); } public void testSetName() { fail("Not yet implemented"); } public void testGetGender() { fail("Not yet implemented"); } public void testSetGender() { fail("Not yet implemented"); } public void testToString() { fail("Not yet implemented"); }

} If you try Run -> Run As -> JUnit Test on the left side of the eclipse window you will get a brown stripe(Window is shown below) indicating that tests have failed, with a cross against all 5 test method names, meaning that all 5 tests failed.

Page 5: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

Click on any one of them and see what message you get. When I clicked on testSertName, I got the error message “Assertion Failed Error: Not yet implemented” (See left bottom of window below) and the corresponding fail statement in the test code is highlighted. This action is always helpful in identifying which assertion failed. Clearly this is what we expect now – we have not written the tests yet!

Page 6: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

Now we have to define the tests and make sure the tests work. In method setup() you define what has to be done before each test is carried out. I am suggesting that we create two Student objects, one with no argument and one with the name and gender supplied. Just to make sure that these objects are available to all the methods, declare these two objects as instance variables of the StudentTest class, so that these objects are accessible by all the test methods. I typed Student s1, s2; protected void setUp() throws Exception { super.setUp(); s1 = new Student(); s2 = new Student("John", 'm'); } This means that, before invoking each test method, the system will create two Student objects s1 and s2. Now we are ready to define the tests for testGetName. I typed public void testGetName() { assertEquals(s1.getName(), "Unknown"); assertEquals(s2.getName(), "John"); }

Page 7: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

I saved the file and then selected Run -> Run As -> JUnit Test. Notice that I forgot to save the test class and the system asks me whether I forgot and invites me to indicate whether it should save before running. I accepted that option. The result is shown below:

We got the same brown stripe indicating some test failures but notice that there is a tick mark beside testGetname, meaning that this test was successful.

We moved on and did the same with the testSetName. Look at the code below – it is easy to follow. public void testSetName() { s1.setName("Mary"); s2.setName("Tom"); assertEquals(s1.getName(), "Mary"); assertEquals(s2.getName(), "Tom"); } Success again with this test method. I did the same with testSetGender and defined the method given below

Page 8: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

public void testGetGender() { s2.setName("Tom"); assertEquals(s1.getGender(), 'X'); assertEquals(s2.getGender(), 'm'); } but got a failure. To analyze what happened, we clicked on testGet Gender in the top left window and on the bottom left window got the message (see below) junit.framework.AssertionFailedError: expected:<x> but was:<X>

I looked at the testGetGender and saw the problem. Now either the test method is wrong or the code in Student.java is wrong. Fix whichever one needs to be fixed. Here I fixed StudentTest class and ran again – success!! The new testGetGender is as follows: public void testGetGender() { s2.setName("Tom"); assertEquals(s1.getGender(), 'X'); assertEquals(s2.getGender(), 'm');

Page 9: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

} I continued with the remaining methods and fixed as needed. I decided to omit the tests for the constructor just because I felt the other methods test the constructors fixing problems as I went. Remember that the problem could be in the test suite or in the class being tested. Finally I got a green stripe meaning that all tests were successful!

Page 10: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

The class StudentTest is finally as follows: package myPackage; import junit.framework.TestCase; public class StudentTest extends TestCase { Student s1, s2; protected void setUp() throws Exception { super.setUp(); s1 = new Student(); s2 = new Student("John", 'm'); }

public void testGetName() { assertEquals(s1.getName(), "Unknown"); assertEquals(s2.getName(), "John"); } public void testSetName() { s1.setName("Mary"); s2.setName("Tom"); assertEquals(s1.getName(), "Mary"); assertEquals(s2.getName(), "Tom"); } public void testGetGender() { s2.setName("Tom"); assertEquals(s1.getGender(), 'x'); assertEquals(s2.getGender(), 'm'); } public void testSetGender() { s1.setName("Mary"); s1.setGender('f'); assertEquals(s1.getGender(), 'f'); } public void testToString() { assertEquals(s1.toString(), "Name and gender unknown"); s1.setName("Mary"); s1.setGender('f'); assertEquals(s1.toString(), "Name Mary - female"); assertEquals(s2.toString(), "Name John - male"); } }

Page 11: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

Test Driven development Now we will take a second example to illustrate Test Driven Development and some other ideas. We want to write a class called CourseOffering. An object of this class corresponds to an offering of a course (e.g., the summer offering of 60-322). Such an object contains the name of the course and the number of available spots in the course. For example, this offering has 36 seats since that is the capacity of this classroom. Rather than write the class CourseOffering and then the tests for the class we will write the tests and then the class.

1) To get things started we defined a JUnitTest called CourseOffering Test in the same package where we have the other classes. To do this we selected myPackage under testJUnit and pressed File -> new -> JUnit Test Case. As before I supplied the name of the test, CourseOfferingTest . Notice I did not say which class I will test since the class (CourseOffering) does not exist yet .

2) The system did not offer to create test methods since I have not specified which class I will

test. I typed the following method: public void testCourseOffering(){ CourseOffering newCourse = new CourseOffering("60-322", 3); // create a new CourseOffering with a new course number and a // class limit of 3 assertEquals("60-322", newCourse.getCourse()); assertEquals(3, newCourse.numSeatsLeft());

} 3) Notice that Eclipse gave an error message since CourseOffering is not

defined yet.

4) I ignored it and pressed Proceed to run it as a JUnit test. I got a failure (Brown stripe in the JUnit stub). Note the error message java.lang.Error: Unresolved compilation problems: CourseOffering cannot be resolved to a type

Page 12: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

5) I called code-assist by clciking on the x in red. I got a number of suggestions (see the window on the right).

Page 13: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

6) I picked ‘Create class CourseOffering’. I selected that and got a

window to create a new Java class.

Page 14: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

7) I picked source folder as src – the same place where the Student

class is.

Page 15: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

8) Now I find that CourseOfferingTest has 3 different errors (See under

Description in window above): The method getCourse() is undefined for the type CourseOffering Description Resource Path Location Type The method numSeatsLeft() is undefined for the type CourseOffering The constructor CourseOffering(String, int) is undefined

9) I tried code-assist for the constructor problem and selected ‘Create Constructor(String, int)’

10) I got the following constructor stub in CourseOffering: public CourseOffering(String string, int i) { // TODO Auto-generated constructor stub

} 11) I completed the constructor as follows:

public CourseOffering(String string, int i) { courseNumber = string; seatsRemaining = i;

} 12) I saved the file and came back to CourseOfferingTest. I found that

the compiler error for the constructor has gone away. 13) However, I now have two problems for the constructor as follows:

Page 16: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class
Page 17: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

14) I tried code-assist for the compiler error for the first error and got the following suggestion:

15) I selected the second suggestion to define an instance variable called courseNumber.

16) In a similar manner I fixed the problem for seats remaining. Now CourseOffering has no errors, so I returned to CourseOfferingTest. To fix the first error I clicked on the error for getCourse.

17) I selected ‘create method getCourse’ as the fix. 18) Eclipse created a method stub which I completed as follows:

public String getCourse() { return courseNumber; }

Page 18: Procedure to create a test class using JUnit 3 using Eclipse · File -> new -> JUnit Test Case. a) Select New JUnit Test 3 test b) Type “StudentTest” as the name of the class

19) I repeated the same with numSeatsLeft(). Notice that the method stub returns an Object not an int. (See window below)

20) I fixed the method as follows:

public int numSeatsLeft() { return seatsRemaining; }

21) I ran the JUnit Test which was successful. 22) Now I have to create other methods as needed.