美文网首页
Testcase Mock

Testcase Mock

作者: nafoahnaw | 来源:发表于2019-04-11 17:15 被阅读0次

    mock私有方法

    https://automationrhapsody.com/mock-private-method-call-powermock/

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
     
    import static org.hamcrest.CoreMatchers.is;
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.mockito.Matchers.anyObject;
    import static org.mockito.Mockito.mock;
     
    @RunWith(PowerMockRunner.class)
    @PrepareForTest(PowerMockDemo.class)
    public class PowerMockDemoTest {
     
        private PowerMockDemo powerMockDemoSpy;
     
        @Before
        public void setUp() {
            powerMockDemoSpy = PowerMockito.spy(new PowerMockDemo());
        }
     
        @Test
        public void testMockPrivateMethod() throws Exception {
            Point mockPoint = mock(Point.class);
     
            PowerMockito.doReturn(mockPoint)
                .when(powerMockDemoSpy, "privateMethod", anyObject());
     
            Point actualMockPoint = powerMockDemoSpy.callPrivateMethod();
     
            assertThat(actualMockPoint, is(mockPoint));
        }
    }
    
    

    mock静态方法

    https://automationrhapsody.com/mock-static-methods-junit-powermock-example/

    package com.automationrhapsody.junit;
     
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
     
    import static org.junit.Assert.assertTrue;
    import static org.mockito.Matchers.anyInt;
    import static org.mockito.Mockito.when;
     
    @RunWith(PowerMockRunner.class)
    @PrepareForTest({Utils.class})
    public class LocatorServiceTest {
     
        private LocatorService locatorServiceUnderTest;
     
        @Before
        public void setUp() {
            PowerMockito.mockStatic(Utils.class);
     
            locatorServiceUnderTest = new LocatorService();
        }
     
        @Test
        public void testGeneratePointWithinDistance() {
            int distance = 111;
     
            when(Utils.randomDistance(anyInt())).thenReturn(distance);
     
            Point input = new Point(11, 11);
            Point expected = new Point(input.getX() + distance, 
                    input.getY() + distance);
     
            assertTrue(arePointsEqual(expected, 
                locatorServiceUnderTest.generatePointWithinDistance(input, 1)));
        }
     
        public static boolean arePointsEqual(Point p1, Point p2) {
            return p1.getX() == p2.getX()
                && p1.getY() == p2.getY();
        }
    }
    

    如果static方法没有返回值
    https://stackoverflow.com/questions/9585323/how-do-i-mock-a-static-method-that-returns-void-with-powermock

    PowerMockito.doNothing().when(FileTransferUtil.class, "transferFile", any(FileStorage.class)
                    , anyString(), any(FileStorage.class), anyString(), anyString(), anyBoolean(), anyString()
                    , anyBoolean(), any(TransferType.class));
    

    校验方法运行的次数

    Object someObject = new Object();
    verify(someObject, times(2)).equals(any(Object.class));
    

    如果需要校验static方法的执行次数
    https://stackoverflow.com/questions/18466198/how-to-verify-static-void-method-has-been-called-with-power-mockito

    如果需要校验private方法的执行次数
    https://www.codota.com/code/java/methods/org.powermock.api.mockito.PowerMockito/verifyPrivate

    SomeClass someClass = spy(new SomeClass());
    someClass.callPrivateMethod();
    verifyPrivate(someClass, times(N)).invoke("somePrivateMethod", any(MethodParameter1.class), any(MethodParameter2.class));
    

    java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer"

    https://stackoverflow.com/questions/16520699/mockito-powermock-linkageerror-while-mocking-system-class
    尝试@PowerMockIgnore("javax.management.*")

    校验运行时方法的参数

    https://stackoverflow.com/questions/3555472/mockito-verify-method-arguments

    public class SomeClass{
        public void someFun(){
            List<String> all = other.fun();
            List<String> wanted = new ArrayList();
            for(String id : all){
                if(check(id)){
                    wanted.add(id);
                }
            }
            other.fun2(wanted);
        }
    }
    

    比如要写测试用例校验other.fun2(wanted)的参数是否是预期中的.
    testcase可以这么些

    ArgumentCaptor<List> argument = ArgumentCaptor.forClass(List.class);
    verify(other).fun2(argument.capture());
    Assert.assertEquals(argument.getValue().size(), wantedLength);
    

    跳过流程中的某些方法

    https://towardsdatascience.com/mocking-a-method-in-the-same-test-class-using-mockito-b8f997916109
    使用spy

    Stack map does not match the one at exception handler

    https://systemoutprint.github.io/2017/09/06/PowerMock%E7%97%9B%E8%8B%A6%E7%9A%84%E6%8E%A5%E5%85%A5%E4%B9%8B%E6%97%85/

    相关文章

      网友评论

          本文标题:Testcase Mock

          本文链接:https://www.haomeiwen.com/subject/dfrxwqtx.html