重新生产的步骤,
使用受保护的方法创建一个类,并在子类中重写相同的方法。
为子类创建一个测试,并尝试抑制方法调用 throws TooManyMethodsFoundException
.
测试接口.java
package com.test.powermock;
/**
* Created by dineshkumar on 06/05/16.
*/
public interface TestInterface {
public int testMethod() throws Exception;
}
抽象测试.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/#!主题/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条答案
按热度按时间wydwbb8l1#
重新描述问题:
使用powermock
suppress + method()
无法正确获取@Overriden protected
方法:voidmethodwithparams,因为问题TooManyMethodsFoundException
,找到类中存在的两个方法ImplClassTest
以及AbstractTest
问题:找到两种方法!powermock不知道该抑制哪一个。解决方法:这个问题肯定难看,用java原创的方式
foo.class.getMethod()
(仅公开)或bar.class.getDeClaredMethod()
(受保护和私人)ps:如果您想在一个类中抑制所有方法,请使用powermock的
methodsDeclaredIn()
```import static org.powermock.api.support.membermodification.MemberMatcher.methodsDeclaredIn;
suppress(methodsDeclaredIn(AbstractTest.class))