我在Mockito中没有做太多的测试,也很少做。当我对某个对象调用delete时,得到一个DeleteResponse
。这有一个称为getProcessingErrors()
的方法,它是一个集合。然后,我可以呼叫.isEmpty()
以查看是否有错误。我正在尝试嘲笑。
DeleteResponse deleteResponse = mock(DeleteResponse.class);
when(catalogFramework.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);
when(deleteResponse.getProcessingErrors()).thenReturn(new HashSet<ProcessingDetails>());
PowerMockito.when(deleteResponse.getProcessingErrors().isEmpty()).thenReturn(true);
错误:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Boolean cannot be returned by getProcessingErrors()
getProcessingErrors() should return Set
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.
现在,从我阅读本文的方式来看,它说
isEmpty
无法返回布尔值,但是我相信它已经过时了,只是查看getProcessingErrors。我怎样才能解决这个问题? 最佳答案
在模拟deleteResponse.getProcessingErrors()
功能之前需要模拟isEmpty()
对象
when(deleteResponse.getProcessingErrors()).thenReturn(mockObject);
PowerMockito.when(mockObject.isEmpty()).thenReturn(true);