zoukankan      html  css  js  c++  java
  • PowerMock与EasyMock的应用

      Leader要求在做Junit测试的时候,Mock掉各个方法之间的依赖。这两天学习了下PowerMock的使用。

      PowerMock是EasyMock的一个扩展,加入了static,final,private,以及constructor的Mock功能。但是PowerMock并没有继承EasyMock的一些测试功能,所以不能取代EasyMock,而单独使用。在很多时候必须要PowerMock和EasyMock相互结合,以及加上WhiteBox,才能更好实现测试。

      1. 必不可少的一步:添加测试运行器@RunWith(PowerMockRunner.class),如果没有添加这个运行器,则使用的是默认的JUnit4.class。在PrepareForTest中加入测试class。@PrepareForTest(测试.class)

      2. Construtor

        public class A{
            public void doA(){
              B.doB();
              }
          }

        public class B{
           public B(){
               System.out.println("can't be here!");
            }
        }
           

        测试classA中的doA方法:

      @RunWith(PowerMockRunner.class)
      @PrepareForTest({A.class})
      public class ATest {

          @Test
          public void testdoA() throws Exception {
              B mockB = PowerMock.createMock( B.class );
              PowerMock.expectNew( B.class ).andReturn(mockB);
              A testA = new A();
              PowerMock.replayAll();
              testA.doA();
              PowerMock.verifyAll();
          }

      }

       3.static

        测试static方法的时候必须要加上@PrepareForTest(静态方法.class),否则就会抛出java.lang.IllegalStateException: no last call on a mock available异常。如果有多个class,用{},例如@PrepareForTest({A.class,B.class})

        public class A{
            public void doA(){
              B.doB();
            }
        }

        public class B{
            public static String doB() {
              return ("can't be here !");
            }
        }

        测试classA中的doA方法:

        @RunWith(PowerMockRunner.class)
        @PrepareForTest({A.class, B.class})
        public class ATest {
            @Test
            public void testdoA() throws Exception {
                PowerMock.mockStatic( B.class );
                EasyMock.expect(B.doB()).andReturn("Mocked !");
                A testA = new A();
                PowerMock.replayAll();
                assertEquals("Mock not complete !", testA.doA(), "Mocked !");
                PowerMock.verifyAll();
            }
        }

       4.mock类中的Field

        一个好的Field应该有getter和setter,但是代码中没有,我们也可以用WhiteBox来Mock掉Field。

        public class A{
            private String C;

            public A(){
                C = "Can't be here !";
            }
        
            public String doA(){
                return C;
            }
        
        }

        测试A中的doA方法

        @RunWith(PowerMockRunner.class)
        @PrepareForTest({A.class, B.class})
        public class ATest {

            @Test
            public void testdoA(){
                A testA = new A();
                String mockC = "Mocked !";
                Whitebox.setInternalState( testA, "C", mockC );
                assertEquals("Mock not complete !", testA.doA(), "Mocked !");
            }

        }

  • 相关阅读:
    Java JMX 监管
    Spring Boot REST(一)核心接口
    JSR 规范目录
    【平衡树】宠物收养所 HNOI 2004
    【树型DP】叶子的颜色 OUROJ 1698
    【匈牙利匹配】无题II HDU2236
    【贪心】Communication System POJ 1018
    【贪心】Moving Tables POJ 1083
    Calling Extraterrestrial Intelligence Again POJ 1411
    【贪心】Allowance POJ 3040
  • 原文地址:https://www.cnblogs.com/charlexu/p/PowerMock.html
Copyright © 2011-2022 走看看