InterviewSolution
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:
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.
If the ASSERTION fails, then an exception will be thrown and the test execution will be stopped.
If the assertion fails, then an exception will be thrown and the test execution will be stopped.
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.
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.
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:
|
|
| 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:
|
|
| 10. |
What are some of the important annotations provided by JUnit? |
|
Answer» Some of the annotations provided by JUnit are as follows:
|
|
| 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:
|
|
| 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:
|
|
| 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. |
|