Creating unit test cases

Maven projects follow certain conventions; the entire application source in a Maven project is in the src/main/java folder, and unit tests are expected to be in the src/test/java folder. In fact, when you create a Maven project in Eclipse, it creates the src/test/java folder for you. We are going to create our test cases in this folder. We are going to create the same package structure for the test classes as that for the application source; that is, to test the packt.book.jee.eclipse.ch5.bean.Course class, we will create the packt.book.jee.eclipse.ch5.bean package under the src/test/java folder and then create a JUnit test class called CourseTest, as follows:

  1. Right-click on the src/test/java folder in Package Explorer in Eclipse and select New | JUnit Test Case (if you do not find this option in the menu, select New | Other, and type junit into the Filter textbox. Then, select the JUnit Test Case option).
  2. Enter the package name as packt.book.jee.eclipse.ch5.bean and the class name as CourseTest.
  1. Click on the Browse... button next to the Class under test textbox. Type course into the Filter textbox and select the Course class:
    Figure 5.1: JUnit test case wizard
    1. Click Next. The page shows methods in the class (Course) for which we want to create the test cases. Select the methods that you want to create test cases for.
    2. We don't want to test getters and setters because they are simple methods and don't do much other than just getting or setting member variables. Presently, we will create a test case for only one method: isValidTestCase. Select the checkbox for this method:
    Figure 5.2: Select methods for test cases
    1. Click Finish. Eclipse checks whether JUnit libraries are included in your project, and if not, prompts you to include them:
      Figure 5.3: Include JUnit libraries in project
      1. Click OK. Eclipse creates the package and the test class with one method/test case called testIsValidCourse. Note that the method is annotated with @Test, indicating that it is a JUnit test case.

      How do we test whether isValidCourse works as expected? We create an instance of the Course class, set some values that we know are valid/invalid, call the isValidateCourse method, and compare the results with the expected results. JUnit provides many methods in the Assert class to compare the actual results obtained by calling test methods with the expected results. So, let's add the test code to the testIsValidCourse method:

      package packt.book.jee.eclipse.ch5.bean; 
      import org.junit.Assert; 
      import org.junit.Test; 
      public class CourseTest { 
       
        @Test 
        public void testIsValidCourse() { 
          Course course = new Course(); 
          //First validate without any values set 
          Assert.assertFalse(course.isValidCourse()); 
          //set  name 
          course.setName("course1"); 
          Assert.assertFalse(course.isValidCourse()); 
          //set zero credits 
          course.setCredits(0); 
          Assert.assertFalse(course.isValidCourse()); 
          //now set valid credits 
          course.setCredits(4); 
          Assert.assertTrue(course.isValidCourse()); 
        } 
       
      } 

      We first create an instance of the Course class, and without setting any of its values, call the isValidCourse method. We know that it is not a valid course because the name and credits are the required fields in a valid course. So, we check whether the returned value of isValidCourse is false by calling the Assert.assertFalse method. We then set the name and check again, expecting the instance to be an invalid course. Then, we set a 0 credits value for Course, and, finally, we set 4 credits for Course. Now, isValidCourse is expected to return true because both the name and credits are valid. We verify this by calling Assert.assertTrue.

      ..................Content has been hidden....................

      You can't read the all page of ebook, please click here login for view all page.
      Reset
      3.21.106.7