• 单元测试之mock使用


    单元测试之mock使用

    https://blog.csdn.net/wohiusdashi/article/details/124085245

    JUnit+mockito+powermock进行可行的单元测试

    https://www.cnblogs.com/candlia/p/11919932.html

      

    junit运行Parameterized参擞化测试
    https://blog.csdn.net/IndexMan/article/details/85037046

    *所有@Test 都是 org.junit.Test
    @Test(expectedExceptions=ExceptionInInitializerError.class)
    public void testcase3(){
    throw new ExceptionInInitializerError();
    }

    单元测试之PowerMock--OK
    https://www.cnblogs.com/kancy/p/13912443.html

    断言assertThat用法--OK
    https://blog.csdn.net/weixin_35569507/article/details/63313802

    JUnit单元测试6—@Rule注解--OK
    https://www.jianshu.com/p/e74ca1b42730

    FixMethodOrder指定测试方法的执行顺序--OK
    https://blog.csdn.net/hehyyoulan/article/details/84959113

    单元测试-PowerMock--OK2
    https://segmentfault.com/a/1190000040144424?sort=votes


    @PowerMockIgnore--OK
    因为PowerMock的工作原理即是使用自定义的类加载器来加载被修改过的类,从而达到打桩的目的
    注释来告诉PowerMock将某个包的加载推迟到系统类加载器。您需要忽略的是特定于案例,但通常是XML框架或与其交互的一些包。
    参考:
    PowerMock注解PowerMockIgnore的使用方法
    https://blog.csdn.net/jackie_xiaonan/article/details/9896739
    PowerMock踩坑指南
    https://www.jianshu.com/p/44fcadaf041d


    整合测试类:Suite.SuiteClasses---OK
    /**
    * 如果是需要多个单元测试类整合测试 使用一个Runner进行异步测试,只需要把相关的class放入到SuiteClasses{}中即可,
    * 如:JunitTest.class 和 TestClassDemo.class 都是写好的单元测试类.
    */
    @RunWith(Suite.class)
    @Suite.SuiteClasses({JunitTest.class, TestClassDemo.class})
    public class AllTestClass {
    //此类的作用是整合测试也称 打包测试;可以把之前所有的写好的test class类进行集成;
    //如需测试多个类时,只需要把相关的测试类加入到"{}"即可;如果不是同一个包类的class记得加上package名称。
    //@Suite.SuiteClasses({JunitTest.class,TestClassDemo.class})
    }

    mockito verify 使用说明---OK
    verify方法用于验证 mock bean 的方法调用,要求必须是mock对象
    Mockito. verify (mockBean ).someMethod();
    表示:someMethod方法调用了一次,相当于times(1)
    Mockito. verify (mock Bean, Mockito.times(n) ).someMethod();
    表示:someMethod方法调用了n次
    Mockito. verify (mock Bean, Mockito.never() ).someMethod();
    表示:someMethod方法未执行
    Mockito. verify (mock Bean, Mockito. atLeastOnce() ).someMethod();
    表示:someMethod方法至少执行过一次,相当于atLeast(1)

    powerMock和mockito使用---OK
    https://blog.csdn.net/terrorblade1235/article/details/108040651
    单元测试实践篇:Mock
    https://blog.csdn.net/Taobaojishu/article/details/108591311

    枚举的使用:
    PowerMock mock enum,这里的 Whitebox.setInternalState 可以设置 TypeEnum fieldName=N 的值为给定的 mock 枚举
    String mockValue = "mock title";
    TypeEnum typeMock = PowerMockito.mock(TypeEnum.class);
    Whitebox.setInternalState(TypeEnum.class, "N", typeMock);
    when(typeMock.getTitle()).thenReturn(mockValue);
    Assert.assertEquals(TypeEnum.N.getTitle(), mockValue);
    Assert.assertEquals(TypeEnum.Y.getTitle(), "TRUE");

    代码覆盖率测试工具: --OK
    jacoco-maven-plugin
    Maven – JaCoCo代码覆盖率示例
    https://www.codenong.com/cs106548758/
    使用jacoco-maven-plugin生成单元测试覆盖率报告
    https://blog.csdn.net/xiaokanfuchen86/article/details/124794210

      


    PrepareForTest的使用场景
    当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。
    当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
    当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。
    当需要mock私有方法的时候, 只是需要加注解@PrepareForTest,注解里写的类是私有方法所在的类
    当需要mock系统类的静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解里写的类是需要调用系统方法所在的类
    @https://blog.csdn.net/qaz296490709/article/details/72880370

  • 相关阅读:
    [zz]struct epoll_event
    [zz]libev 简介
    [zz]红黑树
    [zz]leveldb 实现原理
    [zz]使用 libevent 和 libev 提高网络应用性能
    [zz]AVL树
    [zz]do...while(0)的妙用
    Mybatis中的缓存简介
    Spring框架的介绍
    ThreadLocal
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/16285912.html
Copyright © 2020-2023  润新知