Spring Boot Testing Essentials: JUnit and Mockito Quiz Quiz

Assess your understanding of Spring Boot testing concepts using JUnit and Mockito with this carefully crafted quiz. Sharpen your skills on best practices, annotations, and unit test techniques within the Spring Boot ecosystem.

  1. JUnit Test Annotation

    Which annotation is used to indicate a test method in JUnit 5 when writing unit tests for a Spring Boot application?

    1. @SpringTest
    2. @Exam
    3. @TestMethod
    4. @Test

    Explanation: @Test is the correct annotation used to mark a method as a test case in JUnit 5. @Exam and @TestMethod are not valid JUnit annotations and will not trigger the test framework. @SpringTest is not an official annotation and does not instruct JUnit to run a test. Using @Test ensures your method is recognized and executed as a test case.

  2. Purpose of Mockito

    When writing unit tests, what is the primary purpose of using Mockito in a Spring Boot project?

    1. To deploy the application
    2. To mock dependencies
    3. To increase application speed
    4. To configure the main method

    Explanation: Mockito is mainly used to mock dependencies, allowing for isolation of the unit being tested. It cannot deploy the application or configure the main method directly. Increasing application speed is not the primary function of Mockito; its goal is to simulate components for effective unit testing.

  3. Testing REST Controllers

    Which annotation should you use to create only the web layer context for testing Spring Boot REST controllers?

    1. @DataJpaTest
    2. @RestTest
    3. @SpringBootTest
    4. @WebMvcTest

    Explanation: @WebMvcTest sets up a minimal context containing only the web layer components, making it ideal for controller testing. @SpringBootTest loads the entire context, which is unnecessary for controller-only tests. @DataJpaTest is for JPA repository tests, and @RestTest is not a valid Spring test annotation.

  4. Injecting Mocks

    In a unit test using Mockito, which annotation injects mock objects into the field of the tested class?

    1. @Mock
    2. @MockBean
    3. @Autowired
    4. @InjectMocks

    Explanation: @InjectMocks creates an instance of the class and injects the mocks into it. @Mock creates mock objects but doesn’t inject them. @MockBean is typically used in integration testing with Spring. @Autowired is used for dependency injection in Spring, not specifically for mocks in unit tests.

  5. Verifying Method Calls

    How can you verify that a mocked method was called exactly once in a Mockito-based test?

    1. adding @Spy to the method
    2. checking the console log
    3. using verify(mock).method()
    4. using assertEquals

    Explanation: The verify function in Mockito confirms that a method was called a specific number of times, such as once. Checking the console log is unreliable and not automated. @Spy is used for partial mocks, not direct verification of call counts. assertEquals compares values, not method invocations.

  6. Test Configuration Isolation

    Why is using @SpringBootTest considered less efficient for simple unit tests in Spring Boot?

    1. It disables logging
    2. It skips dependency injection
    3. It loads the entire application context
    4. It causes syntax errors

    Explanation: @SpringBootTest loads the full application context, which is time-consuming and unnecessary for simple unit tests. It does not skip dependency injection; rather, it includes it as part of the context. Logging is unrelated to the inefficiency, and it does not inherently cause syntax errors.

  7. Mocking a Service

    What is the correct way to create a mock for a service class in a standard JUnit 5 test using Mockito?

    1. @TestMock private Service service;
    2. @Mock private Service service;
    3. @ServiceMock private Service service;
    4. @FakeService private Service service;

    Explanation: @Mock creates a mock instance of the service class. The other options, such as @ServiceMock, @FakeService, and @TestMock, are not recognized annotations in Mockito and will not function as intended. Proper use of @Mock helps simulate class behavior for testing.

  8. Testing Repository Logic

    Which annotation is best suited for testing only repository logic and related configuration in Spring Boot?

    1. @DataJpaTest
    2. @SpringBootRepositoryTest
    3. @WebTest
    4. @RestControllerTest

    Explanation: @DataJpaTest configures an in-memory database and repository beans for focused repository testing. @RestControllerTest and @WebTest are not standard annotations and do not target repository logic. @SpringBootRepositoryTest is not a recognized Spring annotation.

  9. Arranging Test Data

    When setting up a JUnit test, which method is commonly used to initialize test data before each test method executes?

    1. @BeforeEach
    2. @PreTest
    3. @AfterEach
    4. @Prepare

    Explanation: @BeforeEach runs before every test method, making it ideal for initializing test data. @AfterEach runs after each test, @Prepare and @PreTest are not valid JUnit annotations and won't trigger setup logic. Using @BeforeEach ensures each test starts with a known state.

  10. Behavior-Driven Development Style

    Which approach in Mockito specifies expected behavior in a BDD (Behavior-Driven Development) manner, often using when-thenReturn or given-willReturn?

    1. Stub-Verify-Repeat
    2. BDDMockito style
    3. Arrange-Act-Assert
    4. API chaining

    Explanation: BDDMockito style uses methods like when-thenReturn or given-willReturn for a readable, behavior-driven approach. Arrange-Act-Assert is a test structure, not a mocking method. API chaining and Stub-Verify-Repeat are not names for this style in Mockito. BDDMockito promotes clear, intention-revealing code in tests.