- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
无论我是否使用,我的测试似乎都通过了
@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/
无论我是否使用,我的测试似乎都通过了 @BeforeEach void initMocks() { MockitoAnnotations.initMocks(this); } 或者不是,我不明
每个人都使用 @Before 而不是 @BeforeClass 与 initMocks 有什么原因吗?就我而言,我在 setup 方法中只有一个方法调用,那就是 MockitoAnnotations.
所以我一整天都在努力让 Mockito 为我的 Android 项目工作。我将所有内容添加到我的 Gradle 构建文件中: androidTestCompile 'org.mockito:mocki
我在仪器测试中使用 @Mock 注释时遇到问题。 这是我的 gradle 依赖项: androidTestCompile 'org.mockito:mockito-core:1.10.19' andr
在编写新的 jUnit4 测试时,我想知道是使用 @RunWith(MockitoJUnitRunner.class) 还是 MockitoAnnotations.initMocks(this)。 我
尝试在我的 AndroidTestCase 中使用 mockito。我将依赖项添加到 build.gradle 中: final DEXMAKER_VERSION = '1.2' dependenci
我是一名优秀的程序员,十分优秀!