Java 用于为私有方法执行任何操作的PowerMockito执行选项
我有一个类,它有两个私有方法,我正在为一个方法test()编写JUnit,希望另一个方法test1什么都不做。它是这样的..我想调用的方法是test(),当它被调用时,我想调用test1(),它被称为Java 用于为私有方法执行任何操作的PowerMockito执行选项,java,unit-testing,exception,powermockito,private-methods,Java,Unit Testing,Exception,Powermockito,Private Methods,我有一个类,它有两个私有方法,我正在为一个方法test()编写JUnit,希望另一个方法test1什么都不做。它是这样的..我想调用的方法是test(),当它被调用时,我想调用test1(),它被称为 class foo{ private void test1() { //some code; } private int test( Element e){ test1(element e); return 0; } } 我的测试课是这样的 @RunWith(PowerMockRunn
class foo{
private void test1() {
//some code;
}
private int test( Element e){
test1(element e);
return 0;
}
}
我的测试课是这样的
@RunWith(PowerMockRunner.class)
@PrepareForTest(Foo.class)
public class FooTest {
private Foo foo= new Foo;
public void testTest() {
element e;
Foo testOb= PowerMockito.spy(foo);
PowerMockito.doNothing().when(testOb, "test1", Matchers.any(Element.class));
PowerMockito.doCallRealMethod().when(testOb, "test", e);
}
现在我面临的问题是,它显示出一个例外
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
now if i remove the "PowerMockito.doNothing().when(testOb, "test1", Matchers.any(Element.class));" line then it works fine but adding this produces the exception can anyone please help me..
尽管您正在模拟的方法返回了一个值,但我看不到您在模拟类时返回值,powermockito框架在模拟时期望返回值,但是我没有看到您发送模拟值,请尝试在类中使用返回模拟值 实现doNothing是错误的
Mockito.doNothing().when(mockedObject.Method() 我正在模拟的返回值的方法将返回一个值,因为尽管我使用power mock返回了任何东西,但实际方法将在那里被调用。。第二个方法的问题是它不会返回任何内容,并且doNothing()行在那里抛出异常。。没有那条线,一切都很好。。但我也不得不嘲笑这种方法。。。