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 !");
            }

        }

  • 相关阅读:
    一段asp程序中效率极低的代码,造成连接数据库超时
    古月照今尘
    with C# Code run on Dynamicx AX 2009
    Using X++ Code export to methods list of Class
    Useing X++ code saved as full Screen Resolution Image
    Find out field list(EDT) on a AX Table
    进制转换
    with tableId and fieldname get to the field value on a Common table records
    Set focus on the dialog field
    Label in Dynamics AX 2009
  • 原文地址:https://www.cnblogs.com/charlexu/p/PowerMock.html
Copyright © 2011-2022 走看看