Java 正在尝试删除一个已经被删除的mock,但我遇到了未完成的stubging异常
这是我对一个方法的测试用例,其中一个值被转换成其他格式。因此,我正在检查expectedConvertedValue和actualConvertedValue setHardCodeValuesToStudentObject()正在为student对象设置一些硬编码的值,例如(name=“jack”、age=“12”等等),在这个方法中有一个mock,并在该mock上发生存根。我需要模拟一个方法,因为在setHardCodeValuesToStudentObject()中,getValue()的硬编码值是自动生成的。当在该行之前添加其他代码行时,它将递增。所以我不得不嘲笑这个价值观 当我试图在这个(setHardCodeValuesToStudentObject())方法中模拟一个方法时,它抛出了这个------>Java 正在尝试删除一个已经被删除的mock,但我遇到了未完成的stubging异常,java,junit,mocking,mockito,powermockito,Java,Junit,Mocking,Mockito,Powermockito,这是我对一个方法的测试用例,其中一个值被转换成其他格式。因此,我正在检查expectedConvertedValue和actualConvertedValue setHardCodeValuesToStudentObject()正在为student对象设置一些硬编码的值,例如(name=“jack”、age=“12”等等),在这个方法中有一个mock,并在该mock上发生存根。我需要模拟一个方法,因为在setHardCodeValuesToStudentObject()中,getValue()的
你试过使用ArgumentCaptor吗?在这种情况下,我如何使用参数捕获器?setHardCodeValuesToStudentObject()正在设置一些值,在该方法中,正在调用另一个方法,该方法将值设置为列表。因此,我希望从该列表中选择一个值。如果需要帮助,请发布所有要测试的代码,因为从测试类中很难确定要测试什么。是否尝试使用ArgumentCaptor?在这种情况下,我如何使用参数捕获器?setHardCodeValuesToStudentObject()正在设置一些值,在该方法中,正在调用另一个方法,该方法将值设置为列表。所以我想从这个列表中得到一个值。如果你需要帮助,那么发布所有你想测试的代码,因为从你的测试类中,很难弄清楚你到底想测试什么。
public void testFunction() throws Exception {
ABC mock1= mock(ABC.class);
when(mock1.getValue()).thenReturn("string1");
whenNew(ABC.class).withNoArguments().thenReturn(mock1);
Student student= setHardCodeValuesToStudentObject();
Method method = Person.class.getDeclaredMethod("conversion",
Student.class);
method.setAccessible(true);
String actualConvertedValue = (String)method.invoke(new Person(),
student);
ClassLoader classLoader = this.getClass().getClassLoader();
InputStream inputStream =
classLoader.getResourceAsStream("expectedConvertedValue.json");
String expectedConvertedValue= "";
try {
expectedConvertedValue= IOUtils.toString(inputStream,
StandardCharsets.UTF-8);
}catch (Exception e) {
System.out.print(e.printStackTrace());
}
assertEquals(expectedConvertedValue,actualConvertedValue );
}
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at
com.print.view.ModelTest.setUpTransactionAnnotation(ModelTest.java:653)
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, you naughty developer!
3: you are stubbing the behaviour of another mock inside before
'thenReturn' instruction if completed