gpt4 book ai didi

java - MockitoAnnotations.initMocks 是否隐含?

转载 作者:行者123 更新时间:2023-12-02 00:59:30 25 4
gpt4 key购买 nike

无论我是否使用,我的测试似乎都通过了

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

或者不是,我不明白为什么,因为在“Pivotal Certified Professional Core Spring 5 Developer”一书中,我正在阅读一个测验的答案,说这是强制性的。

这是相关的完整代码片段

/*
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())
);
}
}

删除 initMocks 方法不会改变任何事情,该代码的其余部分是否以某种方式隐含了初始化?

最佳答案

仅当您不使用 @ExtendWith(MockitoExtension.class) 时才需要

MockitoAnnotations.initMocks(this);。每次调用测试方法时,扩展都会为您执行此操作。请参阅reference docs用于扩展。

关于java - MockitoAnnotations.initMocks 是否隐含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60884709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com