重新提出步骤,
- Create a class with a protected method and override the same in subclass.
- Create a test for the subclass and try to suppress the method call
throws TooManyMethodsFoundException
.
2. 试验接口。
package com.test.powermock;
/**
* Created by dineshkumar on 06/05/16.
*/
public interface TestInterface {
public int testMethod() throws Exception;
}
摘要
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{
}
}
ImplClass Test.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");
}
}
ImplClass Test.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/#
还研究了以下法典:
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);
}
}
我是否可以压制这些方法?