Reputation: 531
I am using Mockito
for mocking services in my current project.
I have a scenario where I need to mock chained methods in code. The chained method uses fluent design pattern. Code is as below. I could not find a solution which satisfies my requirement.
ProcessCall setValue = ProcessCall.url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create();
I am trying to Mock the above code like below
@Test
public void ProcessPost(){
System.out.println("--------------------------");
ProcessCall procCall= Mockito.mock(ProcessCall.class, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(ProcessCall .url("").http(HttpMethod.GET).contentType(null).reqHeaders(null).payload(null).create()).thenReturn(??);
}
Not sure what to pass in thenReturn(??) method.ProcessCall is a class with a private constructor. and it has a execute() method which I need to execute from the result of the call. I am getting the below error:
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Or 'a static method call on a prepared class`
For example:
@PrepareForTest( { StaticService.class })
TestClass{
public void testMethod(){
PowerMockito.mockStatic(StaticService.class);
when(StaticService.say()).thenReturn(expected);
}
}
Also, this error might show up because:
1. inside when() you don't call method on mock but on some other object.
2 . inside when() you don't call static method, but class has not been prepared.
Can somebody help me resolve this issue. I am stuck with this issue and cannot find any proper solution on SO.
Thanks
Upvotes: 2
Views: 6213
Reputation: 26502
I think that the exception says most about the solution.. as you are mocking a static method, then it is advisable to use PowerMockito (you will need to add proper dependency) and then in you test:
@RunWith(PowerMockRunner.class)
@PrepareForTest( { ProcessCall.class })
public class MyTest{
@Test
public void ProcessPost(){
System.out.println("--------------------------");
ProcessCall processCallInstance = ProcessCall.getInstance();
ProcessCall procCall= PowerMockito.mockStatic(ProcessCall.class
, Mockito.RETURNS_DEEP_STUBS);
Mockito.when(ProcessCall .url("").http(HttpMethod.GET)
.contentType(null).reqHeaders(null).payload(null).create())
.thenReturn(processCallInstance);
...
processCallInstance.execute();
...
}
As i assume ProcessCall
is a singleton you would need to use something like ProcessCall.getInstance();
to get an object and then mark it as the result of the deep stub calls.. and then execute whatever you need on it.
Additionally
If you want to mock the execute()
method, then again you can use PowerMockito to achieve this:
@RunWith(PowerMockRunner.class)
@PrepareForTest( { ProcessCall.class })
public class MyTest{
@Test
public void ProcessPost(){
System.out.println("--------------------------");
ProcessCall processCallInstance = PowerMockito.mock(ProcessCall.class);
Upvotes: 1