PowerMock中继承的受保护方法的TooManyMethodsFoundException

时间:2016-05-10 04:18:30

标签: java unit-testing mockito powermock powermockito

重新制作的步骤,

  1. 使用受保护的方法创建一个类,并在子类中覆盖它。
  2. 为子类创建测试,并尝试禁止方法调用throws TooManyMethodsFoundException
  3. TestInterface.java

    package com.test.powermock;
    
    /**
     * Created by dineshkumar on 06/05/16.
     */
    public interface TestInterface {
        public int testMethod() throws Exception;
    }
    

    AbstractTest.java

    package com.test.powermock;
    
    /**
     * Created by dineshkumar on 06/05/16.
     */
    public abstract class AbstractTest implements TestInterface {
    
    public int testMethod() throws Exception {
            return 0;
        }
    
        protected void voidMethodWithParams(String a) throws Exception{
    
        }
    }
    

    ImplClassTest.java

    package com.test.powermock;
    
    /**
     * Created by dineshkumar on 06/05/16.
     */
    public class ImplClassTest extends AbstractTest {
    
    
        @Override
        public int testMethod() throws Exception {
            voidMethodWithParams("a");
            return 2;
        }
    
        @Override
        protected void voidMethodWithParams(String a) throws Exception{
            System.out.println("dd");
        }
    }
    

    ImplClassTestTest.java

    package com.test.powermock;
    
    import org.powermock.api.mockito.PowerMockito;
    import static org.powermock.api.support.membermodification.MemberMatcher.method;
    import static org.powermock.api.support.membermodification.MemberModifier.suppress;
    import static org.testng.Assert.*;
    
    /**
     * Created by dineshkumar on 06/05/16.
     */
    public class ImplClassTestTest {
    
        @org.testng.annotations.Test
        public void testTestMethod() throws Exception {
            ImplClassTest implClassTestSpy = PowerMockito.spy(new ImplClassTest());
            suppress(method(ImplClassTest.class, "voidMethodWithParams", String.class));
            int res = implClassTestSpy.testMethod();
            assertEquals(res, 2);
        }
    }
    

    谷歌之后,https://groups.google.com/forum/#!topic/powermock/h1U5YyEXqfY

    还尝试了以下代码,

    package com.test.powermock;
    
    import org.powermock.api.mockito.PowerMockito;
    
    import static org.powermock.api.support.membermodification.MemberMatcher.method;
    import static org.powermock.api.support.membermodification.MemberModifier.suppress;
    import static org.testng.Assert.*;
    
    /**
     * Created by dineshkumar on 06/05/16.
     */
    public class ImplClassTestTest {
    
        @org.testng.annotations.Test
        public void testTestMethod() throws Exception {
            ImplClassTest implClassTestSpy = PowerMockito.spy(new ImplClassTest());
            suppress(method(AbstractTest.class, "voidMethodWithParams", String.class));
            suppress(method(ImplClassTest.class, "voidMethodWithParams", String.class));
            int res = implClassTestSpy.testMethod();
            assertEquals(res, 2);
        }
    }
    

    我有什么方法可以压制这些方法吗?

1 个答案:

答案 0 :(得分:0)

重新描述问题:

  

使用Powermock 3c/2+1无法正确获取suppress + method()方法: voidMethodWithParams ,因为问题@Overriden protected,找到课堂上存在的两种方法TooManyMethodsFoundExceptionImplClassTest

问题:找到两个方法! Powermock并不知道压制哪一个。

AbstractTest

解决方法:这个问题肯定很难看,使用java原创方式 // encounter TooManyMethodsFoundException !!! suppress(method(AbstractTest.class, "voidMethodWithParams", String.class)); (仅供公开使用)foo.class.getMethod() (适用于受保护和私人)

bar.class.getDeClaredMethod()

PS :如果你想要压制一个类中的所有方法,请使用powermock' s // "voidMethodWithParams" is protected method use getDeclaredMethod() suppress(ImplClassTest.class.getDeclaredMethod("voidMethodWithParams", String.class)) // "publicMethod" use getMethod() suppress(ContainPublicMethodClass.class.getMethod("publicMethod", foo.class))

methodsDeclaredIn()