Introducing JUnit

JUnit test classes are Java classes separate from the classes you want to test. Each test class can contain many test cases, which are just methods marked to be executed when JUnit tests are executed. A test suite is a collection of test classes.

The convention is to assign the test class the same name as that of the class you want to test, and append Test to that name. For example, if you wanted to test the Course class from the previous chapter, then you would create a JUnit test class and name it CourseTest. Test case (method) names start with test, followed by the name of the method in the class that you want to test; for example, if you wanted to test the validate method in the Course class, then you would create the testValidate method in the CourseTest class. Test classes are also created in the same package as the package in which the classes to be tested are present. In Maven projects, test classes are typically created under the src/test/java folder. The convention is to create the same package structure in the test folder as in the src/main/java folder.

JUnit supports annotations to mark unit tests and test suites. Here is a simple test case for the Course class:

/** 
* Test for {@link Course} 
*/ 
Class CourseTest { 
  @Test 
  public void testValidate() { 
    Course course = new Course(); 
    Assert.assertFalse(course.validate()); 
    course.setName("course1") 
    Assert.assetFalse(course.validate()); 
    Course.setCredits(-5); 
    Assert.assetFalse(course.validate()); 
    course.setCredits(5); 
    Assert.assertTrue(course.validate()); 
  } 
} 

Let's assume that the validate method checks that the course name is not null and that credits is greater than zero.

The preceding test case is marked with the @Test annotation. It creates an instance of the Course class, and then calls the Assert.assertFalse method to make sure that the validate method returns false, because name and credits are not set, and they will have their default values, which are null and 0, respectively. Assert is a class provided by the JUnit library, and has many assert methods to test many conditions (see http://junit.sourceforge.net/javadoc/org/junit/Assert.html for more information).

The test case, then, only sets the name, and does the same validation again, expecting the validate method to return false, because the credits are still zero. Finally, the test case sets both the name and credits, and calls the Assert.assertTrue method to ensure that course.validate() returns true. If any of the assertions fail, then the test case fails.

Other than @Test, you can use the following annotations provided by JUnit:

  • @Before and @After: Methods annotated with these annotations are executed before and after each test. You may want to initialize resources in @Before and free them in @After.
  • @BeforeClass and @AfterClass: Similar to @Before and @After, but instead of being called per test, these methods are called once per test class. A method with the @BeforeClass annotation is called before any of the test cases in that class are executed, and one with @AfterClass is called after all the test cases are executed.
..................Content has been hidden....................

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