Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is MockitoAnnotations.initMocks implied?

My tests seems to pass whether I use

@BeforeEach
void initMocks() {
    MockitoAnnotations.initMocks(this);
}

or not and I don't understand why since in the "Pivotal Certified Professional Core Spring 5 Developer" book I am reading an answer of a quizz says it's mandatory.

Here is the full code snippet concerned

/*
Freeware License, some rights reserved

Copyright (c) 2019 Iuliana Cosmina

Permission is hereby granted, free of charge, to anyone obtaining a copy 
of this software and associated documentation files (the "Software"), 
to work with the Software within the limits of freeware distribution and fair use. 
This includes the rights to use, copy, and modify the Software for personal use. 
Users are also allowed and encouraged to submit corrections and modifications 
to the Software for the benefit of other users.

It is not allowed to reuse,  modify, or redistribute the Software for 
commercial use in any way, or for a user's educational materials such as books 
or blog articles without prior permission from the copyright holder. 

The above copyright notice and this permission notice need to be included 
in all copies or substantial portions of the software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS OR APRESS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package com.apress.cems.mockito;

import com.apress.cems.dao.Storage;
import com.apress.cems.repos.StorageRepo;
import com.apress.cems.services.impl.SimpleStorageService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.*;

import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

/**
 * @author Iuliana Cosmina
 * @since 1.0
 * Description: new-style using Mockito mocks with JUnit 5
 */
@ExtendWith(MockitoExtension.class)
class SimpleStorageServiceTest3 {
    static final Long STORAGE_ID = 1L;

    @Mock //Creates mock instance of the field it annotates
    private StorageRepo mockRepo;

    @InjectMocks
    private SimpleStorageService storageService;

    @BeforeEach
    void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    void findByIdPositive() {
        var storage = new Storage();
        storage.setId(STORAGE_ID);
        when(mockRepo.findById(any(Long.class))).thenReturn(Optional.of(storage));

        Storage result = storageService.findById(STORAGE_ID);

        verify(mockRepo, times(1)).findById(any(Long.class));
        assertAll(
                () -> assertNotNull(result),
                () -> assertEquals(storage.getId(), result.getId())
        );
    }
}

Removing the initMocks method doesn't change a thing, is that initialization implied in some sort of way by the rest of that code ?

like image 546
ORL Avatar asked Aug 24 '26 23:08

ORL


2 Answers

MockitoAnnotations.initMocks(this); is only required if you are not using @ExtendWith(MockitoExtension.class). The extension does it for you each time a test method is invoked. See reference docs for the extension.

like image 116
VinPro Avatar answered Aug 26 '26 14:08

VinPro


use MockitoAnnotations.openMocks(this); instead.

like image 44
Galley Avatar answered Aug 26 '26 14:08

Galley