Junit Interview Questions And Answers

junit interview questions and answers   Top 13 Junit Interview Questions And Answers

1) What is JUnit?

It is an open source Unit testing framework for Java. It is used to test small chunk of code. Developers who are following test-driven methodology must write and execute unit test first before any code. JUnit promotes the idea of 'first testing then coding', which emphasizes on setting up the test data for a piece of code that can be tested first and then implemented.

2) Why we need JUnit?

● JUnit is useful for developers, who work in a test-driven environment.
● It finds bugs early in the code, which makes code more reliable.
● Unit testing forces a developer to read code more than writing.

3) What are the different features of JUnit?

Following are the features of JUnit:

● JUnit is an open source framework.
● Provides annotations to identify test methods.
● Provides assertions for testing expected results.
● Provides test runners for running tests.
● JUnit tests allow you to write codes faster, which increases quality of the code.
● JUnit is elegantly simple. It is less complex and takes less time.
● In JUnit we can easily identify the passed and failed test cases.

4) How to setup JUnit?

Download the latest version of JUnit jar file and set the JUNIT_HOME environment variable to point to the base directory location where JUNIT jar is stored on your machine. Then set the CLASSPATH environment variable to point to the JUNIT jar location.

5) Can JUnit be integrated?

Yes, JUnit Framework can be easily integrated with either of the following:
● Eclipse
● Ant
● Maven

6) What are Fixtures in JUnit?

Fixtures is a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well-known & fixed environment in which tests are run so that results are repeatable.

7) Which are the different methods of Fixtures in JUnit?

Fixtures include following methods:

● setUp() method, which runs before every test invocation.
● tearDown() method, which runs after every test method.

8) Write down the code snippet of Fixtures in JUnit?

Here is the code snippet:

import junit.framework.*;

public class JavaTest extends TestCase {
   protected int val1, val2;
   // assigning the values
   protected void setUp(){
      val1 = 3;
      val2 = 3;

   // test method to add two values
   public void testAdd(){
      double result = val1 + val2;
      assertTrue(result == 6);

9) What is the Test Suite in JUnit?

A test suite bundles a few unit test cases and runs them together. In JUnit, both @RunWith and @Suite annotation are used to run the suite test.

10) Write down the code snippet of Test Suite in JUnit?

Here is the code snippet:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

//JUnit Suite Test sample

   TestJunit1.class ,TestJunit2.class

public class JunitTestSuite {

11) What is the Test Runner in JUnit?

Test runner is used for executing the test cases.

12) Write down the code snippet of Test Runner in JUnit?

Here is the code snippet:

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {

13) List down some important JUnit classes?

Following are some of the important JUnit classes:

Assert ? Contains a set of assert methods.
TestCase ? Contains a test case that defines the fixture to run multiple tests.
TestResult ? Contains methods to collect the results of executing a test case.