栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

@ Mock,@ MockBean和Mockito.mock()之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

@ Mock,@ MockBean和Mockito.mock()之间的区别

普通Mockito库

import org.mockito.Mock;...@MockMyService myservice;

import org.mockito.Mockito;...MyService myservice = Mockito.mock(MyService.class);

来自Mockito库,功能等效。
它们允许模拟类或接口,并记录和验证其行为。

使用注释的方式更短,因此更可取,并且通常是更可取的。


请注意,要在测试执行期间启用Mockito批注,

MockitoAnnotations.initMocks(this)
必须调用静态方法。
为了避免测试之间的副作用,建议在每次测试执行之前先进行以下操作:

@Before public void initMocks() {    MockitoAnnotations.initMocks(this);}

启用Mockito批注的另一种方法是

@RunWith
通过指定
MockitoJUnitRunner
执行此任务的,以及其他有用的东西来注释测试类:

@RunWith(org.mockito.runners.MockitoJUnitRunner.class)public MyClassTest{...}

Spring Boot库包装Mockito库

这确实是Spring Boot类:

import org.springframework.boot.test.mock.mockito.MockBean;...@MockBeanMyService myservice;

该类包含在

spring-boot-test
库中。

它允许在Spring中添加Mockito模拟

ApplicationContext

如果上下文中存在与声明的类兼容的bean,则将其 替换 为模拟。
如果不是的话,它 增加了 模拟的背景下,作为一个bean。

Javadoc参考:

可用于将模拟添加到Spring ApplicationContext的注释。

如果在上下文中定义的任何现有的相同类型的单个Bean将被模拟代替,如果没有定义现有的Bean,则将添加一个新的。


什么时候使用经典/普通Mockito以及何时

@MockBean
从Spring Boot 使用?

单元测试旨在与其他组件隔离地测试组件,并且单元测试也有一个要求:执行时间要尽可能快,因为这些测试每天可能在开发人员计算机上执行数十次。

因此,这是一个简单的准则:

当您编写不需要从Spring Boot容器中获取任何依赖项的测试时,遵循经典/普通Mockito的方法是:它快速且有利于隔离测试组件。
如果您的测试需要依赖于Spring Boot容器, 并且 您还想添加或模拟其中一个容器bean:

@MockBean
从Spring
Boot就是这样。


Spring Boot的典型用法

@MockBean

当我们编写一个带有

@WebMvcTest
(Web测试片)注释的测试类时。

Spring Boot文档很好地总结了这一点:

通常

@WebMvcTest
将仅限于单个控制器,并结合使用
@MockBean
以为所需的协作者提供模拟实现。

这是一个例子:

import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mockito;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;import org.springframework.boot.test.mock.mockito.MockBean;import org.springframework.http.MediaType;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;@RunWith(SpringRunner.class)@WebMvcTest(FooController.class)public class FooControllerTest {    @Autowired    private MockMvc mvc;    @MockBean    private FooService fooServiceMock;    @Test    public void testExample() throws Exception {         Foo mockedFoo = new Foo("one", "two");         Mockito.when(fooServiceMock.get(1))     .thenReturn(mockedFoo);         mvc.perform(get("foos/1") .accept(MediaType.TEXT_PLAIN)) .andExpect(status().isOk()) .andExpect(content().string("one two"));    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/365649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号