Prepare Interview

Exams Attended

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address
Withoutbook LIVE Mock Interviews

JUnit Interview Questions and Answers

Freshers / Beginner level questions & answers

Ques 1. What is JUnit?

JUnit is a software testing framework for unit testing, open Source Software maintained by the JUnit.org community. JUnit was originally written by Erich Gamma and Kent Beck.

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

Is it helpful? Add Comment View Comments
 

Ques 2. Who Should Use JUnit?

JUnit is mostly used by developers for testing their written code. 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.

Is it helpful? Add Comment View Comments
 

Ques 3. Why Do You Use JUnit to Test Your Code?

► Using JUnit makes unit testing easier and faster.
► Writing more tests will make more productive, not less productive.
► Unit Tests should be done as soon as possible at the code unit level so at that point we capture the issue and fix it.

Is it helpful? Add Comment View Comments
 

Ques 4. How To Compile a JUnit Test Class?

As JUnit code is written in java, 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 like junit.jar etc. For example, to compile the test class LoginTest.java described previously, you should do this:

javac -cp junit-4.4.jar LoginTest.java

and it will create .class file.
LoginTest.class

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

Is it helpful? Add Comment View Comments
 

Ques 5. How To Write a Simple JUnit Test Class?

You should be able to write this simple test class with one test method:


import org.junit.*;

public class LoginTest

{

@Test public void testLogin()

{

String username = \"withoutbook\";

Assert.assertEquals(\"withoutbook\", username);

}

}


Here first argument in assertEquals is the known parameter which should be equal to username. If both are same it will send me true and if both are not equal sends me false.

Is it helpful? Add Comment View Comments
 

Ques 6. 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 LoginTest.java described previously, you should do this:

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

JUnit version 4.4
Time: 0.015
OK (1 test)

This output says that 1 tests performed and passed. The same you can perform by executing build.xml also.

Is it helpful? Add Comment View Comments
 

Ques 7. What CLASSPATH Settings Are Needed to Run JUnit?

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 should be there.
* 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 found NoClassDefFoundError in your test results, 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:\A\junit-4.4.jar;c:\B\test_classes;
c:\B\target_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:
/C/target_classes:

Is it helpful? Add Comment View Comments
 

Ques 8. How Do I Run JUnit Tests from Command Window?

You need to check the following list to run JUnit tests from a command window:

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

Is it helpful? Add Comment View Comments
 

Ques 9. How To Write a JUnit Test Method?

* You need to mark the method as a JUnit test method with the JUnit annotation: @org.junit.Test.
* A JUnit test method must be a "public" method. This allows the runner class to access this method.
* A JUnit test method must be a "void" method. The runner class does not check any return values.
* A JUnit test should perform one JUnit assertion - calling an org.junit.Assert.assertXXX() method.

Here is a simple JUnit test method:

import org.junit.*;
@Test public void testLogin() {
String username = "withoutbook";
Assert.assertEquals("withoutbook", username);
}

Is it helpful? Add Comment View Comments
 

Ques 10. How to create a Test Suite using JUnit in Eclipse?

There are four ways to create a JUnit test suite class in Eclipse with JUnit plugin: org.junit_3.8.1. First, select the directory (usually unit-tests) that you wish to create the test suite class in.

1. Select File > New > Other... > Java > JUnit > JUnit Test Suite.
2. Select the arrow of the button in the upper left of the toolbar. Select Other... > Java > JUnit > JUnit Test Suite,
3. Right click on a package in the Package Explorer view in the Java Perspective, and select Other... > Java > JUnit > JUnit Test Suite,
4. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestSuite as the super class of the test class you are creating.

Is it helpful? Add Comment View Comments
 

Ques 11. How To Create Test Class in Eclipse?

1. Select File ; New ; JUnit Test Case.
2. Select the arrow of the button in the upper left of the toolbar. Select JUnit Test Case.
3. Right click on a package in the Package Explorer view in the Java Perspective, and select JUnitTestCase.
4. Click on the arrow of the icon in the toolbar. Select JUnit Test Case.
5. You can create a normal Java class as shown in the Eclipse tutorial, but include junit.framework.TestCase as the super class of the test class you are creating.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

Related interview subjects

JUnit interview questions and answers - Total 24 questions
Spring Framework interview questions and answers - Total 53 questions
Java Design Patterns interview questions and answers - Total 15 questions
Java 17 interview questions and answers - Total 20 questions
Core Java interview questions and answers - Total 306 questions
Tomcat interview questions and answers - Total 16 questions
Apache Wicket interview questions and answers - Total 26 questions
Java Applet interview questions and answers - Total 29 questions
JAXB interview questions and answers - Total 18 questions
JMS interview questions and answers - Total 64 questions
Log4j interview questions and answers - Total 35 questions
Struts interview questions and answers - Total 84 questions
RMI interview questions and answers - Total 31 questions
Apache Camel interview questions and answers - Total 20 questions
JDBC interview questions and answers - Total 27 questions
Java 11 interview questions and answers - Total 24 questions
JPA interview questions and answers - Total 41 questions
EJB interview questions and answers - Total 80 questions
GWT interview questions and answers - Total 27 questions
Kotlin interview questions and answers - Total 30 questions
Glassfish interview questions and answers - Total 8 questions
Google Gson interview questions and answers - Total 8 questions
Java 21 interview questions and answers - Total 21 questions
JSP interview questions and answers - Total 49 questions
J2EE interview questions and answers - Total 25 questions
Apache Tapestry interview questions and answers - Total 9 questions
Java Swing interview questions and answers - Total 27 questions
Java Mail interview questions and answers - Total 27 questions
Hibernate interview questions and answers - Total 52 questions
JSF interview questions and answers - Total 24 questions
Java 8 interview questions and answers - Total 30 questions
Java 15 interview questions and answers - Total 16 questions
JBoss interview questions and answers - Total 14 questions
Web Services interview questions and answers - Total 10 questions
RichFaces interview questions and answers - Total 26 questions
Servlets interview questions and answers - Total 34 questions
Java Beans interview questions and answers - Total 57 questions
Spring Boot interview questions and answers - Total 50 questions
©2023 WithoutBook