Writing a simple test case
In JUnit, a test case is a class that may contain one or more tests. The purpose of the test case is to group a set of tests together, typically based on their function. For example, you may want to place all tests for a calendar widget in the same class, potentially allowing you to write private methods that can be shared among the tests.
In the example, you'll write a test case that tests basic math functionality, giving you a feel for how to use JUnit. You begin by creating a new class named Math-TestCase and having it extend JUnit's TestCase class, as shown in listing 16.1.
The next step is to add some tests to the class. For each test you add, you need to create a method. The method name must begin with the name test. JUnit works by interrogating the methods in the class; any method that begins with the word test is taken to be a unit test. In listing 16.1, you create four methods, each of which tests a different mathematical function.
Post a comment