0

I've not done much testing and very minimal in Mockito. When I call delete on a certain object, I get a DeleteResponse. This has a method called getProcessingErrors() which is a set. I can then call .isEmpty() to see if there are errors or not. I'm trying to mock this out.

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);

Error:

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.

Now, from the way I am reading this, it is saying isEmpty cannot return a boolean, but I believe it's cutting out early and just looking at the getProcessingErrors. How can I fix this?

2
  • why are you mocking the actual response and then testing that itself. your test case will always evaluate to true. which class and which method are you testing? Commented May 4, 2018 at 7:04
  • Well sometimes I want isEmpty to be true and sometimes I want it to be false. Then I can just throw that one line in each test. No? Commented May 4, 2018 at 7:06

3 Answers 3

3

You need mock deleteResponse.getProcessingErrors() object before mock isEmpty() function

when(deleteResponse.getProcessingErrors()).thenReturn(mockObject);
PowerMockito.when(mockObject.isEmpty()).thenReturn(true);
Sign up to request clarification or add additional context in comments.

Comments

1

In here

when(deleteResponse.getProcessingErrors()).thenReturn(new HashSet<ProcessingDetails>());

PowerMockito.when(deleteResponse.getProcessingErrors().isEmpty()).thenReturn(true);

Either return Set with at least 1 element so that it returns true and you don't need the second line or Mock the HashSet also to return true when isEmpty() is called.

Without HashSet mocking

Set<ProcessingDetails> set = new HashSet<>();
set.add(new ProcessingDetails());
when(deleteResponse.getProcessingErrors()).thenReturn(set);

With HashSet mocking

HashSet<ProcessingDetails> mockedSet = mock(HashSet.class);
when(mockedSet.isEmpty()).thenReturn(true);
when(deleteResponse.getProcessingErrors()).thenReturn(set);

Comments

0

You should be able to use Mockito's RETURNS_DEEP_STUBS to eliminate the need to mock the intermediate object:

DeleteResponse deleteResponse = mock(DeleteResponse.class, RETURNS_DEEP_STUBS);

when(catalogFramework.delete(any(DeleteRequest.class))).thenReturn(deleteResponse);

when(deleteResponse.getProcessingErrors().isEmpty()).thenReturn(true);

Although in this case, using RETURNS_DEEP_STUBS isn't really necessary... you can just initialize an empty HashSet and return it as follows:

when(deleteResponse.getProcessingErrors()).thenReturn(new HashSet<>());

This HashSet is empty, so it will return your desired result of true when calling isEmpty().

2 Comments

Can this be used in conjunction with PowerMockito?
Yes, you should be able to use this with PowerMockito in the way I described above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.