首页 > 其他分享 >单元测试 - Mockito - 2

单元测试 - Mockito - 2

时间:2023-12-25 22:57:02浏览次数:49  
标签:Mockito void random 单元测试 Assertions 注解 mock

3. Mockito 中常用注解

3.1 可以代替 Mock 方法的 @Mock 注解

Shorthand for mocks creation - @Mock annotation

Important! This needs to be somewhere in the base class or a test runner:

快速 mock 的方法,使用 @mock 注解。

mock 注解需要搭配 MockitoAnnotations.openMocks(testClass) 方法一起使用。

@Mock
private Random random;

@Test
void check() {
    MockitoAnnotations.openMocks(this);
    Mockito.when(random.nextInt()).thenReturn(100);
    Assertions.assertEquals(100, random.nextInt());
}

3.2 @BeforeEach 与 @BeforeAfter 注解

@Mock
private Random random;

@BeforeEach
void setUp() {
    System.out.println("----测试开始----");
}

@Test
void check() {
    MockitoAnnotations.openMocks(this);
    Mockito.when(random.nextInt()).thenReturn(100);
    Assertions.assertEquals(100, random.nextInt());
}

@AfterEach
void after() {
    System.out.println("----测试结束----");
}

而在 Junit5 中,@Before 和 @After 注解被 @BeforeEach@AfterEach 所替代。

3.3 Spy 方法与 @Spy 注解

spy() 方法与 mock() 方法不同的是

  1. 被 spy 的对象会走真实的方法,而 mock 对象不会
  2. spy() 方法的参数是对象实例,mock 的参数是 class

示例:spy 方法与 Mock 方法的对比

@Test
void check() {
    CheckAuthorityImpl checkAuthority = Mockito.spy(new CheckAuthorityImpl());
    int res = checkAuthority.add(1, 2);
    Assertions.assertEquals(3, res);

    CheckAuthorityImpl checkAuthority1 = Mockito.mock(CheckAuthorityImpl.class);
    int res1 = checkAuthority1.add(1, 2);
    Assertions.assertEquals(3, res1);
 }

输出结果

// 第二个 Assertions 断言失败,因为没有给 checkAuthority1 对象打桩,因此返回默认值
org.opentest4j.AssertionFailedError: 
Expected :3
Actual   :0

使用 @Spy 注解代码示例

@Spy
private CheckAuthorityImpl checkAuthority;

@BeforeEach
void setUp() {
    MockitoAnnotations.openMocks(this);
}

@Test
void check() {
    int res = checkAuthority.add(1, 2);
    Assertions.assertEquals(3, res);
}

标签:Mockito,void,random,单元测试,Assertions,注解,mock
From: https://www.cnblogs.com/otf-notes/p/17927151.html

相关文章

  • Java Junit 单元测试使用示范(带截图)
    依赖包junit-4.7.jar@Test注解packagejunit;importorg.junit.Test;/***选中add,右键运行add()*/publicclassDemo1{@Testpublicvoidadd(){inta=10;intb=13;intsum=a+b;System.out.println(sum);}......
  • Github Copilot生成代码和单元测试并执行
    ChatGPTPrompts整理总结 最近一直在学习ChatGPTPrompt的编写技巧,做了一些验证和整理,分享给大家ActasaLinuxTerminal英文PromptIwantyoutoactasalinuxterminal.Iwilltypecommandsandyouwillreplywithwhattheterminalshouldshow.Iwantyouto......
  • python自动化学习笔记4-----pytest单元测试框架
            ......
  • java中的单元测试
    通过单元测试,能更好的控制代码质量,提升代码质量,及时准确地定位bug;在java中,JUit是最常用的单元测试工具,我们简单介绍一下他的使用:测试类的基础结构:importorg.junit.Test;importstaticorg.junit.Assert.*;publicclassMyTest{@TestpublicvoidtestAddition(){......
  • cmake应用:集成gtest进行单元测试
    编写代码有bug是很正常的,通过编写完备的单元测试,可以及时发现问题,并且在后续的代码改进中持续观测是否引入了新的bug。对于追求质量的程序员,为自己的代码编写全面的单元测试是必备的基础技能,在编写单元测试的时候也能复盘自己的代码设计,是提高代码质量极为有效的手段。在本系......
  • 在Python中进行自动化单元测试的教程
    一、软件测试大型软件系统的开发是一个很复杂的过程,其中因为人的因素而所产生的错误非常多,因此软件在开发过程必须要有相应的质量保证活动,而软件测试则是保证质量的关键措施。正像软件熵(softwareentropy)所描述的那样:一个程序从设计很好的状态开始,随着新的功能不断地加入,程序逐渐地......
  • 解决程序员单元测试效率问题,AI是助力神器
    我们说了很久的AI智能,那到底作为一种智能化的工具能给我们带来什么样的帮助呢?今天我就从一名程序员的角度来分析一下,实际上AI到底能解决我们什么问题?首先介绍一下单测,单侧的全称叫做单元测试。就是我们在写完代码以后,给我们的最小的模块或者函数来写测试用例。测试用例,主要是用......
  • 单元测试 - Mockito - 1
    1.为什么要使用mockMock可以理解为创建一个虚假的对象,或者说模拟出一个对象,在测试环境中用来替换掉真实的对象,以达到我们可以:验证该对象的某些方法的调用情况,调用了多少次,参数是多少给这个对象的行为做一个定义,来指定返回结果或者指定特定的动作2.Mockito中常用方法2.......
  • 【转载】Springboot2.x单元测试
    参考https://blog.csdn.net/wangxi06/article/details/114630426https://blog.csdn.net/qq_44381387/article/details/120869168(新版spring-boot-starter-test不再集成junit,而是junit-jupiter,无需@RunWith)https://www.jianshu.com/p/34f57f41af70https://www.cnblogs.co......
  • 【转载】SpringBoot2.x使用Assert校验(非单元测试)
    参考https://blog.csdn.net/yangshangwei/article/details/123105926环境环境版本操作windows10JDK11Springboot2.3.12.RELEASE注意引入的包为importorg.springframework.util.Assert;介绍对象和类型断言函数说明notNull()假设对......