JUnit Interview Questions and Answers
Ques 1. What is JUnit?
Ans. 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?Ans. 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?Ans. ► 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?Ans. 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.
The compilation is ok, if you see the LoginTest.class file.
Ques 5. How To Write a Simple JUnit Test Class?Ans. You should be able to write this simple test class with one test method:
public class LoginTest
@Test public void testLogin()
String username = \\\"withoutbook\\\";
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.
Most helpful rated by users: