Interview Questions

What Is JUnit TestCase?

JUnit Questions and Answers

(Continued from previous question...)

What Is JUnit TestCase?

JUnit TestCase is the base class, junit.framework.TestCase, used in JUnit 3.8 that allows you to create a test case. TestCase class is no longer supported in JUnit 4.4.

A test case defines the fixture to run multiple tests. To define a test case

  • Implement a subclass of TestCase
  • Define instance variables that store the state of the fixture
  • Initialize the fixture state by overriding setUp
  • Clean-up after a test by overriding tearDown

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

import junit.framework.*;
// by
public class MathTest extends TestCase {
    protected double fValue1;
    protected double fValue2;

    protected void setUp() {
        fValue1= 2.0;
        fValue2= 3.0;
    public void testAdd() {
        double result= fValue1 + fValue2;
        assertTrue(result == 5.0);

(Continued on next question...)

Other Interview Questions