Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

JUnit Interview Questions and Answers

Ques 6. How To Run a JUnit Test Class?

Ans. 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 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?
Ans. 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;

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:
Is it helpful? Add Comment View Comments
Ques 8. How Do I Run JUnit Tests from Command Window?
Ans. 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?
Ans. * 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. Why Not Just Use a Debugger for Unit Testing?
Ans. * Automated unit testing requires extra time to setup initially. But it will save your time, if your code requires changes many times in the future.
* A debugger is designed for manual debugging and manual unit testing, not for automated unit testing. JUnit is designed for automated unit testing.
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2020 WithoutBook