Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How does JUnit help in achieving tests isolation?

Answer»

For calling test methods, JUnit creates SEPARATE individual INSTANCES of the test class. For example, if the test class contains 10 TESTS, then JUnit creates 10 instances of the class to execute the test methods. In this way, every test is ISOLATED from the other.

2.

What is the importance of @RunWith annotation?

Answer»

@RunWith annotation LETS us run the JUnit tests with other custom JUnit RUNNERS like SpringJUnit4ClassRunner, MockitoJUnitRunner ETC. We can also do parameterized TESTING in JUnit by making use of @RunWith(Parameterized.class).

3.

What are the JUnit Assert Methods?

Answer»

Assert methods are utility methods that support assert conditions in test cases. They belong to the Assert class in JUnit 4 and the Assertions class in JUnit 5. It is recommended to import the assert methods statically to the test class for avoiding using the class as a prefix to the method. Let us consider an example of the Multiplier class:

public class Multiplier{ public int multiply(int num1, int num2){ return num1 * num2; }}

Following are some of the assert methods:

  • assertEquals(): This method compares 2 objects for equality by making use of the equals() method of the object. This is shown in the test case that multiplies 2 methods and checks for the expected and actual value below.
@Test @DisplayName("Multiplying 2 numbers") public void multiplyTest() { assertEquals(50, multiplier.multiply(10, 5)); }

       When two objects are found to be equal based on the equals() method implementation of the object, then assertEquals() returns normally. Else, an exception will be thrown and the test will stop its execution.

  • assertTrue(): This method tests whether the value of a variable is true.
@Test public void checkNumTest() { int num1 = 20; assertTrue("Number is not equal to 0", num1!=0);}

   If the ASSERTION fails, then an exception will be thrown and the test execution will be stopped.   

  • assertFalse(): This method tests whether the value of a variable is FALSE.
@Test public void checkNumTest() { int num1 = -20; assertFalse("Number is not greater than 0",num1>0);}

   If the assertion fails, then an exception will be thrown and the test execution will be stopped.

  • assertNull(): This method tests if a variable is null. If null, it returns normally else an exception will be thrown and the test stops execution.
@Test public void checkNullTest() { int num1 = null; assertNull("Number is null",num1);}
  • assertNotNull(): This method tests whether a variable is not null. If it is not null, then the test returns normally else an exception will be thrown and the test stops its execution.
@Test public void checkNotNullTest() { int num1 = null; assertNotNull("Number is null",num1);}
  • assertSame(): This method checks if two references of the object are pointing to the same object.
@Test public void checkAssertSameTest() { Object num1 = new Object(); Object num2 = new Object(); assertSame(num1, num2); }

   If the object references are pointing to the same object, the test runs normally else, it will throw an exception and the test execution is aborted.

  • assertNotSame(): This method checks if two references of an object are not pointing to the same object.
@Test public void checkAssertSameTest() { Object num1 = new Object(); Object num2 = new Object(); assertSame(num1, num2);}

   If the object references are not pointing to the same object, the test runs normally else, it will throw an exception and the test execution is aborted.   

  • assertThat(): This method compares the object to org.hamcrest.Matcher for checking if it matches WHATEVER the Matcher class requires for the object to match. Consider the following CODE.
import static org.hamcrest.MatcherAssert.assertThat;import static org.hamcrest.Matchers.hasItems;import org.junit.jupiter.api.Test;public class MultiplierTest { @Test public void assertThatTest() { assertThat( Arrays.asList(1,2,3,4), hasItems(2,3)); }}

In this code, we will be asserting that the list has some items specified in the hamcrest’s hasItems() method. If the assertThat is successful, i.e if the list indeed has items specified, then the test runs normally. Else, the test throws an exception and the test stops executing.

4.

What is mocking and stubbing?

Answer»

Mocking is a phenomenon where an object mimics a real object. WHEREAS, STUBBING are the codes RESPONSIBLE for taking place of another component. Mockito, EASYMOCK are some of the mocking frameworks in JAVA.

5.

What is a test suite?

Answer»

A test suite is a bundle of multiple UNIT test cases which can be run TOGETHER. The following image represents how to test suite LOOKS like:

We can use @RunWith and @Suite ANNOTATIONS over the test class for running the test suite.

6.

What is a JUnit fixture?

Answer»

FIXTURE represents a fixed state of object sets used as a baseline for running test methods. This is to ensure there is a fixed and well-known environment where the results of the test methods are repeatable when RUN multiple TIMES. The fixture has the FOLLOWING 2 methods:

  • SETUP()This runs before every test case is run.
  • tearDown() This method is run after every test is run.
7.

What is the importance of @Test annotation?

Answer»

@TEST ANNOTATION is USED for MARKING the METHOD as a test method.

8.

What will happen if the return type of the JUnit method is String?

Answer»

The test CASE execution will fail because the JUnit METHODS are DESIGNED to RETURN VOID.

9.

How will you write a simple JUnit test case?

Answer»

Let us understand how to WRITE a simple JUnit Test Case using an example. Consider a Calculator class that has a method that adds 2 numbers:

public class Calculator { public INT add(int num1, int num2) { return num1 + num2; }}

Let us write a test case in JUnit 5 to this method under a class named CalculatorTest.

import static org.junit.jupiter.api.Assertions.assertEquals;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.DISPLAYNAME;import org.junit.jupiter.api.RepeatedTest;import org.junit.jupiter.api.Test;public class CalculatorTest { Calculator calcObject; @BeforeEach void setUp() { calcObject = new Calculator(); } @Test @DisplayName("Add 2 numbers") void addTest() { assertEquals(15, calcObject.add(10, 5)); } @RepeatedTest(5) @DisplayName("Adding a number with zero to return the same number") void testAddWithZero() { assertEquals(15, calcObject.add(0, 15)); }}

From the above example, we can see that:

  • @BeforeEach annotated method runs before each test case. In JUnit 4, this annotation was @Before.
  • @Test annotation indicates that the method is a test method.
  • @DisplayName is used for defining the test name displayed to the user.
  • assertEquals() method is used for validating whether the expected and actual values are equal.
  • @RepeatedTest annotation indicates that the test method will be run 5 times.
10.

What are some of the important annotations provided by JUnit?

Answer»

Some of the annotations provided by JUnit are as follows:

  • @Test: This annotation over a public method of void RETURN type can be run as a test case. This is a replacement of the org.junit.TestCase annotation.
  • @Before: This is used when we want to execute the preconditions or any initialisation based statements before running EVERY test case.
  • @BeforeClass: This is used when we want to execute statements before all test cases. The statements MAY include test connections, common setup initialisation etc.
  • @After: This is used when we want to execute statements after each test case. The statements can be resetting the variables, deleting extra memory used etc.
  • @AfterClass: This is used when certain statements are REQUIRED to be executed after all the test cases of the class are run. Releasing resource connections post-execution of test cases is one such example.
  • @IGNORES: This is used when some statements are required to be ignored during the execution of test cases.
  • @Test(timeout=x): This is used when some timeout during the execution of test cases is to be set. The value of x is an integer that represents the time within which the tests have to be completed.
  • @Test(expected=NullPointerException.class): This is used when some exception thrown by the target method needs to be asserted.
11.

Is it mandatory to write test cases for every logic?

Answer»

No, it is not mandatory. However, TEST cases can be WRITTEN for the logic which can be REASONABLY broken and TESTED INDEPENDENTLY.

12.

What are the features of JUnit?

Answer»

Following are the features of JUnit:

  • JUnit is an open-source framework.
  • Supports automated testing of test suites.
  • Provides annotations for identifying the test METHODS.
  • Provides ASSERTIONS to test expected results or exceptions of the methods under test.
  • Provides a platform for RUNNING test cases automatically and CHECKING their results and giving feedback.
13.

Why do we use JUnit? Who uses JUnit more - Developers or Testers?

Answer»

JUnit is used more often by developers for implementing unit test cases for the functionalities they have developed. However, these days testers also use this FRAMEWORK for performing unit testing of the applications. 

JUnit is used due to the following reasons:

  • Helps in automating test cases.
  • Helps in reducing defects in the code LOGIC whenever the logic changes.
  • Helps in reducing the cost of testing as the bugs are identified, captured and addressed at early PHASES in the software development.
  • Helps to IDENTIFY the gaps in CODING and gives a chance to refactor the code.
14.

What is Unit Testing?

Answer»

UNIT testing is a SOFTWARE testing strategy that tests single entities like methods or classes at a time. This helps to ensure that the product quality is met as per the business requirements. It also helps in reducing the technical debt of the APPLICATION by HELPING developers discover issues in the code logic due to any changes. It also gives insights into how the code logic implemented COULD impact future changes. 

The lifecycle of the unit testing process is as shown in the image below:

15.

What is JUnit?

Answer»

JUnit is an open-source, Java-based unit testing framework that plays a crucial ROLE in achieving the culture of TDD (Test Driven Development). The TDD culture lays strong emphasis on setting up the test data for testing a logic that would be implemented once the testing is successful. JUnit helps to increase the software stability as it helps in identifying the bug in the code logic at an early stage WITHOUT requiring the software to GO to production. This helps in reducing the time required to debug any ISSUES later on.