JUnit Interview Questions and Answers

Hello Friends,

You might be aware of JUnit. As a Java developer sometimes you need to write JUnit tests. So when you go for an interview you might be asked about knowledge of JUnit testing. Some people more focus on testing while taking an interview and not having much knowledge of JUnit might become your weak point.

So here I am listing some frequently asked JUnit interview questions and answers. Let's go one by one...

1) What is Junit ?

Ans - JUnit is a testing framework for unit testing. It uses Java as a programming platform.

2) Explain what is Unit Test Case ?

Ans - Unit Test Case is a part of the code that ensures that the another part of code (method) behaves as expected. For each requirement, there must be at least two test cases one negative test and one positive test.

3) What is use of @Ignore ?

Ans - When your code is not ready, and it would fail if executed then you can use @Ignore annotation.
It will not execute a test method annotated with @Ignore
It will not execute any of the test methods of test class if it is annotated with @Ignore

4) How is the testing of the 'protected' method done ?

Ans - To test the protected method, the test class is declared in the same package as the target class.

5) How is the testing of 'private' method done ?

Ans - There is no direct way for testing of the private method; hence manual testing is to be performed, or the method is changed to "protected" method.

6) What is the test suit ?

Ans - The test suit allows us to group multiple test cases so that it can be run together. TestSuit is the container class under junit.framework.TestSuite package.

7) What does test runner ?

Ans - The test runner is used to execute the test cases.

8) How will you run JUnit from command window ?

Ans - 
1) First set the ClassPath as follows:

and then

2) Invoke JunitCore

java org.junit.runner.JUnitCore

9) What is Parameterized test in JUnit and what all annotations used for this ?

Ans - Parameterized tests are possible in JUnit and they provides us the liberty of passing parameters into the test classes.

@RunWith(Parameterized.class) – For making a class parametrized.

@Parameters – Parameterized class must have a static method for generating and returning a collection of array, this method should be marked with @Parameters annotation.

10) What is use of @Rule ?

Ans - It intercepts the test method thus providing an opportunity to do some stuff before or after the execution of a particular test method.

11) Can we change return type of JUnit test method from void to some other type ?

Ans - Ideally you should not do this. All the JUnit test methods should have a void return type. If you change the return type then the test method would not be considered as a test method and would be ignored during execution of tests.

12) How @Test annotation is used for testing exceptions ?

Ans - @Test (expected = Exception.class)
But there is one limitation - It is used for testing only a single exception.

13) What happens if a Junit test method is declared as “private” ?

Ans - If a Junit test method is declared as “private”, the compilation will pass ok. But the execution will fail. This is because Junit requires that all test methods must be declared as “public”.

14) The methods Get () and Set () should be tested for which conditions ?

Ans - Unit tests performed on java code should be designed to target areas that might break. Since the set() and get() methods on simple data types are unlikely to break, there is no need to test them explicitly. On the other hand, set() and get() methods on complex data types are vulnerable to break. So they should be tested.

15) What is spy ?

Ans - It is real object with some mock methods and some fake.
Spy is a type of partial mock
This way you can have original behaviour for "cheap" methods and fake behaviour for "expensive" or complex methods.

16) List out some useful JUnit extensions ?

Ans - 

  • Cactus
  • JWebUnit
  • XMLUnit
  • MockObject
17) In what order is multiple @Before annotated methods run ?

Ans - It is not certain which @Before annotated method will run first as all run randomly.

18) Name few Java Mocking frameworks ?

Ans - Here are - Mockito, PowerMock, EasyMock, JMock, JMockit

19) What is the use of Mockito.any ?

Ans - In case we need to verify that a method is being called with any argument and not a specific argument we can use Mockito.any(Class), Mockito.anyString, Mockito.anyLong etc.

20) What should I do if I want to make sure that a particular method of a class is getting called ?

Ans - If its a static method of the class , we can use verify to make sure its getting called.

If its an instance method , We can mock the object and then use verify with the mocked object to make sure that the method is getting called.

21) Have you ever tried mocking static methods ?

Ans - Yes, that can be done using Power Mock. Mockito doesnt provide a way to mock static methods.

22) What are limitations of Mockito ?

Ans - 
  • Cannot mock static methods.
  • Cannot mock constructors.
  • Cannot mock equals(), hashCode().
23) Which frameworks can support mocking Private and Static methods ?

Ans - Frameworks like PowerMockito (which are essentially extensions of Mockito framework), JMockit etc. do provide means to mock private and static methods.