Top Interview JUnit/Mockito Question

  1. What is Unit Testing?
  2. What are the Benefits of Unit Testing?
  3. What is the Mockito framework?
  4. What is mocking?
  5. What are the steps to be performed for the Junit with Mocking framework?
  6. Name Few Java Mocking Frameworks?
  7. When is Mocking required?
  8. What is the benefits of Mockito?
  9. List some Mockito Annotations?
  10. What are the limitations of Mockito?
  11. What is EasyMock?
  12. What is the difference between Assert and Verify?
  13. What is the use of @Spy annotation?
  14. What is the difference between @InjectMocks and @mock?
  15. What Is The Use Of Mockito.any?
  16. Do You Mock Classes & Interfaces?
  17. Can We Mock Static Methods?
  18. How do you mock static methods?
  19. Can you mock private methods using mockito?
  20. What is ArgumentCaptor in Mockito?
  21. What is the Difference Between @Mock and @Spy?
  22. How Do You Handle Exceptions in JUnit Tests?

 

1. What is Unit Testing?

Key Points:

  • Unit Testing is a method of testing the smallest piece of code called a unit.
  • Unit testing of an object is done during the development of an application or project.
  • The main aim is to isolate each unit of the system to identify, analyze and fine-tune the defects.
  • A unit is referred to as an individual function or procedure (program). The developers usually perform it during testing.

2. What are the benefits or advantages of Unit Testing?

Key Points:

  • Early Bug Detection: Unit testing aids in the early detection of flaws or defects in the software code, helping to avoid them developing into larger problems or spreading to later stages of the software development cycle. This reduces the entire development time and lowers costs.
  • Quicker Software Development: Unit tests help engineers find and quickly fix errors, which speeds up software development. Unit testing also aids in the early detection of flaws, which makes it simpler to fix problems before they worsen.
  • Higher Quality Code: Unit testing helps guarantee that code is of a high standard and complies with the specifications of the software. Early bug detection allows engineers to create more dependable, scalable, and effective code.
  • Better Team Communication: Unit testing gives team members a clear and concise way to discuss the code, which enhances team communication. Developers can cooperate to make sure their code complies with the criteria by having an easy time understanding what is expected of their code.
  • Code Reusability: Unit testing can assist in locating code that is applicable to different areas of the programmed. Developers can increase the code's modularity and make it simpler to maintain and modify in the future by spotting these code snippets early on.
  • Better Documentation: Unit tests acts as documentation which shows how the code is supposed to operate. These tests can be used by developers as a guide for comprehending the code, which can assist prevent misunderstandings and confusion.

3. What is the Mockito framework?

Key Points:

  • Mockito is a mocking framework.
  • It is a Java-based library used to create simple and basic test APIs for performing unit testing of Java applications.
  • It can also be used with other frameworks such as JUnit and TestNG.

4. What is mocking?

Key Points:

  • Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects.
  • Mocking is a process of developing the objects that act as the clone of the real objects.
  • Mocking is a testing technique where mock objects are used instead of real objects for testing purposes. Mock objects provide a specific (dummy) output for a particular (dummy) input passed to it.

5. What are the Steps to be Performed for JUnit with Mocking Framework?

 To use JUnit with a mocking framework like Mockito, follow these steps:

  • Add Dependencies: Include JUnit and Mockito dependencies in your project.
  • Annotate Tests: Use annotations like @RunWith(MockitoJUnitRunner.class), @Mock, and @InjectMocks.
  • Create Mocks: Create mock objects using Mockito.mock() or annotations.
  • Define Behavior: Use when() and thenReturn() to define mock behavior.
  • Write Tests: Write test methods to verify the behavior of the code under test.
  • Verify Interactions: Use verify() to ensure that the expected interactions occurred.

6. Name Few Java Mocking Frameworks?

 Some popular Java mocking frameworks include:

  • Mockito: Known for its simple syntax and ease of use.
  • EasyMock: Allows recording and replaying expectations.
  • JMock: Provides a flexible API for setting expectations and verification.

7. When is Mocking Required?

 Mocking is required when:

  • You need to isolate the unit of code under test.
  • The real objects are not available or are impractical to use.
  • The real objects have undesirable side effects.
  • You want to simulate different scenarios or edge cases.

8. What are the Benefits of Mockito?

 Mockito offers several benefits:

  • Simplifies Mock Creation: Provides simple and readable syntax for creating mocks.
  • Reduces Boilerplate Code: Annotation-based configuration reduces boilerplate.
  • Flexible Stubbing: Allows flexible stubbing of methods.
  • Seamless Integration: Works well with JUnit and other testing frameworks.

9. List Some Mockito Annotations?

 Common Mockito annotations include:

  • @Mock: Creates a mock object.
  • @InjectMocks: Injects mocks into the tested object.
  • @Spy: Creates a spy object that allows partial mocking.
  • @Captor: Creates an ArgumentCaptor instance.

10. What are the Limitations of Mockito?

 Mockito has some limitations:

  • Static Methods: Cannot mock static methods directly (requires PowerMockito or other frameworks).
  • Private Methods: Cannot mock private methods directly.
  • Final Classes/Methods: Cannot mock final classes or methods by default (requires configuration).

11. What is EasyMock?

 EasyMock is another mocking framework for Java that allows you to create mock objects and define their behavior using a record-replay-verify model.

Key Points:

  • Provides a different approach to mocking compared to Mockito.
  • Allows recording of expected method calls and replaying them during testing.

12. What is the Difference Between Assert and Verify?

 

  • Assert: Used to check if a condition is true. If not, the test fails.
  • Verify: Used to check if certain methods were called on the mock object with specified arguments.

13. What is the Use of @Spy Annotation?

 The @Spy annotation creates a spy object, which is a real instance of a class but allows you to stub some methods and verify interactions.

Key Points:

  • Useful for partial mocking.
  • Allows calling real methods on the object.

14. What is the Difference Between @InjectMocks and @Mock?

  • @Mock: Creates a mock object.
  • @InjectMocks: Injects mock objects into the class being tested, allowing for dependency injection.

15. What is the Use of Mockito.any?

 Mockito.any is used to match any argument in a method call during stubbing or verification.

Key Points:

  • Simplifies the setup of stubs.
  • Useful for handling variable input values.

16. Do You Mock Classes & Interfaces?

 Yes, you can mock both classes and interfaces. Mocking interfaces is common because it allows testing without relying on concrete implementations.

17. Can We Mock Static Methods?

 Mockito alone does not support mocking static methods directly. For this, you can use PowerMockito, an extension of Mockito.

18. How Do You Mock Static Methods?

 Using PowerMockito, you can mock static methods as follows:

  • Add Dependencies: Include PowerMockito dependencies.
  • Prepare Class: Use @PrepareForTest to specify the class containing static methods.
  • Mock Static Methods: Use PowerMockito.mockStatic() to mock static methods.

19. Can You Mock Private Methods Using Mockito?

 Mockito does not support mocking private methods directly. You can use PowerMockito to achieve this by using reflection.

20. What is ArgumentCaptor in Mockito?

 ArgumentCaptor is used to capture arguments passed to methods in mock objects for further assertions.

Key Points:

  • Captures argument values for verification.
  • Useful for asserting complex argument values.

21. What is the Difference Between @Mock and @Spy?

  • @Mock: Creates a mock object with all methods stubbed.
  • @Spy: Creates a real instance of the class but allows selective method stubbing and verification.

22. How Do You Handle Exceptions in JUnit Tests?

 You can handle exceptions in JUnit tests using the @Test(expected = Exception.class) annotation or by using assertThrows method in JUnit 5.

 

Mastering Unit Testing with JUnit and Mockito:

  • Be familiar with setting up and using Mockito and JUnit in different scenarios.
  • Practice writing tests that cover both positive and negative cases.
  • Understand how to use various Mockito annotations and their purposes.
  • Be ready to explain the principles of unit testing, mocking, and TDD.

No comments:

Post a Comment