mockito入门

文章讲述了在SpringBoot项目中,由于完整的测试套件运行时间过长,作者转向使用Mockito进行单元测试。文章详细展示了如何引入Mockito库,特别是处理静态方法的模拟,并解释了Mockito-inline的作用,以及在遇到依赖注入问题时如何配置和解决。此外,还讨论了在大型项目中使用功能测试和模拟对象的优点。
摘要由CSDN通过智能技术生成

前言

最近在项目中跑单元测试发现直接使用springboot自带的测试,一整套跑起来花费数十分钟,这是无法忍受的,考虑到功能的特殊性,想到了Spring测试包自带的mockito单元测试,所以进行初次尝试使用。

测试代码

pom包
在这里插入图片描述

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

<dependency>
   <groupId>org.mockito</groupId>
   <artifactId>mockito-inline</artifactId>
   <version>4.5.1</version>
   <scope>test</scope>
</dependency>
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * @author Steven
     * @Date 2023/1/30 15:45
     */
    @Service
    public class OrderA {

        @Autowired
        private OrderC orderC;

        public int print() {
            System.out.println("D = "+ OrderD.getResult());
            System.out.println(orderC.print(2));
            System.out.println("hello world");
            return -1;
        }

    }

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * @author Steven
     * @Date 2023/1/30 15:46
     */
    @Service
    public class OrderB {

        @Autowired
        private OrderA order;

        public boolean test() {
            System.out.println("order B test()");
            System.out.println("order value = " + order.print());
            System.out.println("order B hello world");
            return true;
        }

    }

    import org.springframework.stereotype.Service;

    /**
     * @author Steven
     * @Date 2023/1/30 16:43
     */
    @Service
    public class OrderC {

        public int print(int a) {
            System.out.println("hello"+ a);
            return -1;
        }
    }
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    /**
     * @author Steven
     * @Date 2023/1/30 17:32
     */
    @Component
    public class OrderD {
        private static OrderC orderC;

        @Autowired
        public OrderD(OrderC orderCa) {
            System.out.println("==================");
            orderC = orderCa;
        }

        public static int getResult(){
            System.out.println("hhhhh====" + OrderE.print());
            return orderC.print(3);
        }

    }

    /**
     * @author Steven
     * @Date 2023/1/31 14:04
     */
    public class OrderE {

        public static int print() {
            System.out.println("ahhahahahahhaha");
            return 1111111;
        }
        
    }

主要测试类

    import com.timelinecapital.web.service.order.*;
    import org.checkerframework.checker.units.qual.A;
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.Test;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.mockito.MockedStatic;
    import org.mockito.Mockito;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.mock.mockito.MockBean;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit.jupiter.SpringExtension;

    import static org.mockito.ArgumentMatchers.anyInt;
    import static org.mockito.BDDMockito.then;
    import static org.mockito.Mockito.when;

    /**
     * @author Steven
     * @Date 2023/1/30 15:48
     */
    @ContextConfiguration(classes = {OrderB.class, OrderA.class, OrderC.class, OrderD.class})
    @ExtendWith(SpringExtension.class)
    public class OrderTest {

        @Autowired
        private OrderB orderB;

        @MockBean
        private OrderC orderC;


        @Test
        public void testOrder() throws Exception {
            Mockito.mockStatic(OrderE.class);
    //        try (MockedStatic<OrderE> orderEMockedStatic = Mockito.mockStatic(OrderE.class)) {
                when(OrderE.print()).thenReturn(333);
    //            orderEMockedStatic.close();
    //        }
    //        when(orderA.print()).thenReturn(100);
    //        when(orderC.print(anyInt())).thenReturn(101);
            when(orderC.print(anyInt())).thenReturn(201);

            boolean res = orderB.test();

            Assertions.assertTrue(res);
        }

    }

测试代码说明:

@ExtendWith(SpringExtension.class) 这个注解重要是继承spring的环境

@ContextConfiguration 这个注解主要
    - classes属性主要是导入springbean,如果不配置需要倒入的bean会报依赖注入异常

默认情况需要手动配置相关需要注入bean的类否则会报错,依赖注入异常,找不到相应的bean

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderB': Unsatisfied dependency expressed through field 'order'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.timelinecapital.web.service.order.OrderA' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)

有些时间需要mock静态方法,单纯的使用Mockito.mockStatic(OrderE.class);会报错,需要导入mockito-inline包,具体原因后续分析
常见的错误异常有,这错误异常很友好都提示了如何操作:

org.mockito.exceptions.base.MockitoException: 
The used MockMaker SubclassByteBuddyMockMaker does not support the creation of static mocks

Mockito's inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the 'mockito-inline' artifact where you are currently using 'mockito-core'.
Note that Mockito's inline mock maker is not supported on Android.

总结

默认情况下需要手动配置相对应的所有需要注入的bean,凡是你这个模块需要倒入的bean你都需要配置,如果你不配置注入就会报错,要么注入,要么mock,比如OrderB里面依赖了OrderA和一个静态的OrderD类,OrderA bean要么mock要么注入,OrderD就不一样了因为是静态的,但是导入的时候需要注入OrderC所以站在Spring的角度,都需要注入。当然也可以一开始就mock,当一个实例被mock之后他就成虚拟的了,他的依赖就不需要进行注入了。
站在项目的角度,项目越大不可能为了某个功能跑一次服务,因为服务还有其他功能,所以功能测试就成了很好的解决方案,你可以随意的mock,返回想要的值,最大角度的覆盖所有测试,唯一的缺点是,你需要考虑所有的依赖注入。

引用

使用Mockito模拟Static静态方法 https://blog.csdn.net/qq_38646452/article/details/124943944

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

查拉图斯特拉talk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值