SpringBoot 使用Mock单元测试

03-25 1502阅读 0评论

测试一般分为两种黑盒测试和白盒测试。

SpringBoot 使用Mock单元测试,SpringBoot 使用Mock单元测试,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,方法,功能,第1张
(图片来源网络,侵删)

        黑盒测试又称为功能测试或数据驱动测试,测试过程中,程序看作成一个黑色盒子,看不到盒子内部代码结构。

        白盒测试又称为结构测试或逻辑驱动测试,测试过程中,程序看作一个透明盒子,能够看清盒子内部的代码和结构,这样测试人员对程序代码的逻辑有所知晓。

1. Junit 的注解

@Test:使用该注解标注的public void方法会表示为一个测试方法;

@BeforeClass:表示在类中的任意public static void方法执行之前执行;

@AfterClass:表示在类中的任意public static void方法之后执行;

@Before:表示在任意使用@Test注解标注的public void方法执行之前执行;

SpringBoot 使用Mock单元测试,SpringBoot 使用Mock单元测试,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,方法,功能,第2张
(图片来源网络,侵删)

@After:表示在任意使用@Test注解标注的public void方法执行之后执行;

2. Mock模拟测试

mock就是创建一个类的虚假的对象,在测试环境中,用来替换掉真实的对象,只是模拟执行,并不会真正的执行。

  • @InjectMocks:通过创建一个实例,它可以调用真实代码的方法,其余用@Mock(或@Spy)注解创建的mock将被注入到用该实例中。

  • @Mock:对函数的调用均执行mock(即虚假函数),不会调用真实的方法。

  • @Spy:对函数的调用均执行真正部分。

    3.常用的 Mockito 方法

    • do/when:包括doThrow(…).when(…)/doReturn(…).when(…)/doAnswer(…).when(…)
    • given/will:包括given(…).willReturn(…)/given(…).willAnswer(…)
    • when/then: 包括when(…).thenReturn(…)/when(…).thenAnswer(…)/when(…).thenThrow(…)

      4. Springboot测试

      对于Springboot有些属性没办法Mock,比如@Value的注解。

      SpringBoot 使用Mock单元测试,SpringBoot 使用Mock单元测试,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,方法,功能,第3张
      (图片来源网络,侵删)

      但是可以使用

      ReflectionTestUtils.setField(aService, "bService", bServiceMock);

      5. 测试代码 

      public class AService {
          BService bService;
          public String aCallB() {
              String bResult = bService.bCallBaidu();
              return "这是a;b返回:" + bResult;
          }
      }
      public class BService {
          @Autowired
          RestTemplate restTemplate;
          public String bCallBaidu() {
              ResponseEntity responseEntity = restTemplate.exchange("www.baidu.com", HttpMethod.POST, new HttpEntity(""), String.class);
              return "这是b call baidu的结果:" + responseEntity;
          }
      }
      
      @RunWith(MockitoJUnitRunner.class)
      public class AServiceTest {
          @InjectMocks
          AService aService;
          /**
           * @Mock 只是初始化一个模拟的实例化对象,并不会执行实例化的方法
           */
          @Mock
          BService bServiceMock;
          /**
           * @InjectMocks 实例化一个对象,并且会执行实例化对象的方法
           */
          @InjectMocks
          BService bServiceInjectMock;
          @Mock
          RestTemplate restTemplate;
          @Test
          public void testMock() {
              ReflectionTestUtils.setField(aService, "bService", bServiceMock);
              String result = aService.aCallB();
              System.out.println(result);
              Mockito.doReturn("模拟B返回").when(bServiceMock).bCallBaidu();
              result = aService.aCallB();
              System.out.println(result);
          }
          @Test
          public void testInjectMocks() {
              ReflectionTestUtils.setField(aService, "bService", bServiceInjectMock);
              // 如果不对restTemplate进行Mocke处理,bServiced的则会异常
      //        String result = aService.aCallB();
      //        System.out.println(result);
              Mockito.doReturn(new ResponseEntity("RestTemplate模拟返回成功", HttpStatus.OK))
                      .when(restTemplate)
                      .exchange(Mockito.anyString(), Mockito.eq(HttpMethod.POST), Mockito.any(HttpEntity.class), Mockito.eq(String.class));
              String result = aService.aCallB();
              System.out.println(result);
          }
      }

      SpringBoot 使用Mock单元测试

      参考:

       在SpringBoot中编写Mock单元测试 - 简书

      SpringBoot——单元测试实践总结_springboot集成test框架单元测试-CSDN博客


免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1502人围观)

还没有评论,来说两句吧...

目录[+]