Freshers / Beginner level questions
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
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.
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.
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.
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.
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.
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:
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
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);
}
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.
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.
Most helpful rated by users:
- Who Should Use JUnit?
- What is JUnit?
- How To Compile a JUnit Test Class?
- Why Do You Use JUnit to Test Your Code?
- How To Write a Simple JUnit Test Class?