我有一个如下所示的类:
public class ClassOne {
public void function1(InputStream input, OutputStream output, Context context) {
.....
function2(List, String, String);
}
private void function2(List, String, String){...}
}我正在尝试为这个类编写单元测试,如下所示:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassOne.class)
public class ClassOneTest {
private ClassOne classVar;
private ClassOne classSpy;
@Before
public void setup() {
classVar = new ClassOne();
classSpy = new ClassOne(classVar);
}
@Test
public void testFunction1() {
....
PowerMokito.doNothing().when(classSpy, "function2", List, string, string);
}
}我在上面的单元测试中得到了这个错误:
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at org.powermock.core.classloader.ClassloaderWrapper.runWithClassClassLoader(ClassloaderWrapper.java:51)
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();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed我看了几篇帖子,但到目前为止都没有什么帮助。任何帮助都将不胜感激!
发布于 2020-06-30 04:53:50
您没有调用spy(),这导致了问题
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassOne.class)
public class ClassOneTest {
private ClassOne classVar;
private ClassOne classSpy;
@Before
public void setup() {
classVar = new ClassOne();
classSpy = spy(new ClassOne(classVar));
}
@Test
public void testFunction1() {
// Arrange
PowerMokito.doNothing().when(classSpy, "function2", List, string, string);
// Act
....
}
}https://stackoverflow.com/questions/62645571
复制相似问题