Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

JUnit Interview Questions and Answers

Ques 1. 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 2. 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 3. 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 4. 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 5. 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
 

Most helpful rated by users:

©2024 WithoutBook