JUnit Interview Preparation Guide
Download PDF

JUnit Interview Questions and Answers will guide us now that JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks collectively known as xUnit that originated with SUnit. So learn JUnit by this JUnit Interview Questions with Answers guide

45 JUnit Questions and Answers:

1 :: What Is JUnit?

What is JUnit? You need to remember the following points when answering this question:

► It is a software testing framework to for unit testing.
► It is written in Java and designed to test Java applications.
► It is an Open Source Software maintained by the JUnit.org community.

Answer from the JUnit FAQ:

JUnit is a simple, open source framework to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:

► Assertions for testing expected results
► Test fixtures for sharing common test data
► Test runners for running tests

JUnit was originally written by Erich Gamma and Kent Beck.

2 :: Where Do You Download JUnit?

Where do I download JUnit? I don't think anyone will ask this question in a job interview. But the answer is simple. You should follow the download instructions from the JUnit official Website: JUnit.org.

3 :: Who Should Use JUnit, Developers or Testers?

I should say that JUnit is mostly used by developers. JUnit is designed for unit testing, which is really a coding process, not a testing process.

But many testers or QA engineers, are also required to use JUnit for unit testing. For example, I found this job title on the Internet: Lead QA Engineer - Java / J2EE / whitebox / SAP / Junit

4 :: Why Do You Use JUnit to Test Your Code?

This is a commonly asked question in a job interview. Your answer should have these points:

► I believe that writing more tests will make me more productive, not less productive.
► I believe that tests should be done as soon as possible at the code unit level.
► I believe that using JUnit makes unit testing easier and faster.

5 :: How Do You Install JUnit?

How do I install JUnit? First I will download the lastest version of JUnit.

Then I will extract all files from the downloaded file.

The most important file should be the JUnit JAR file: junit-4.4.jar, which contains all JUnit class packages.

To verify junit-4.4.jar, I will run the org.junit.runner.JUnitCore class:

java -cp junit-4.4.jar org.junit.runner.JUnitCore

JUnit version 4.4

Time: 0

OK (0 tests)

6 :: How To Wirte a Simple JUnit Test Class?

This is a common test in a job interview. You should be able to write this simple test class with one test method:

import org.junit.*;

public class HelloTest {
@Test public void testHello() {
String message = "Hello World!";
Assert.assertEquals(12, message.length());

7 :: How To Compile a JUnit Test Class?

Compiling a JUnit test class is like compiling any other Java classes. The only thing you need watch out is that the JUnit JAR file must be included in the classpath. For example, to compile the test class HelloTest.java described previously, you should do this:

javac -cp junit-4.4.jar HelloTest.java

dir HelloTest.*
453 HelloTest.class
183 HelloTest.java

The compilation is ok, if you see the HelloTest.class file.

8 :: How To Run a JUnit Test Class?

A JUnit test class usually contains a number of test methods. You can run all test methods in a JUnit test class with the JUnitCore runner class. For example, to run the test class HelloTest.java described previously, you should do this:

java -cp .;
junit-4.4.jar org.junit.runner.JUnitCore HelloTest

JUnit version 4.4
Time: 0.015

OK (1 test)

This output says that 1 tests performed and passed.

9 :: What CLASSPATH Settings Are Needed to Run JUnit?

It doesn't matter if you run your JUnit tests from a command line, from an IDE, or from "ant", you must define your CLASSPATH settings correctly. Here is what recommended by the JUnit FAQ with some minor changes:

To run your JUnit tests, you'll need the following elemements in your CLASSPATH:

* The JUnit JAR file.
* Location of your JUnit test classes.
* Location of classes to be tested.
* JAR files of class libraries that are required by classes to be tested.

If attempting to run your tests results in a NoClassDefFoundError, then something is missing from your CLASSPATH.

If you are running your JUnit tests from a command line on a Windows system:

set CLASSPATH=c:Ajunit-4.4.jar;c:Btest_classes;

If you are running your JUnit tests from a command line on a Unix (bash) system:

export CLASSPATH=/A/junit-4.4.jar:/B/test_classes:

10 :: How Do I Run JUnit Tests from Command Window?

To run JUnit tests from a command window, you need to check the following list:

1. Make sure that JDK is installed and the "java" command program is accessible through the PATH setting. Type "java -version" at the command prompt, you should see the JVM reports you back the version string.

2. Make sure that the CLASSPATH is defined as shown in the previous question.

3. Invoke the JUnit runner by entering the following command:

java org.junit.runner.JUnitCore <test class name>