TestCase defines a test case with the fixture to run multiple tests.
org.junit.TestCase class id declared as follows.
public abstract class TestCase extends Assert implements Test
A test case defines the fixture to run multiple tests.
The follow list has some frequently used methods from TestCase class.
The following code shows how to use TestCase
import junit.framework.TestCase; //from w w w . ja va2 s.c o m import org.junit.Before; import org.junit.Test; public class TestJunit extends TestCase { protected double fValue1; protected double fValue2; @Before public void setUp() { System.out.println("setup"); fValue1= 2.0; fValue2= 3.0; } @Test public void testAdd() { //count the number of test cases System.out.println("No of Test Case = "+ this.countTestCases()); //test getName String name= this.getName(); System.out.println("Test Case Name = "+ name); //test setName this.setName("testNewAdd"); String newName= this.getName(); System.out.println("Updated Test Case Name = "+ newName); assertEquals(5.0, fValue1+fValue2); } //tearDown used to close the connection or clean up activities public void tearDown( ) { System.out.println("clean up."); } }
Here is the code to run the Test case defined above.
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; // w ww . j av a 2s. co m public class Main { public static void main(String[] args) { Result result = JUnitCore.runClasses(TestJunit.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }
The code above generates the following result.