javaAndBeyond
javaAndBeyond

Reputation: 540

Mocking a static method chain

I am trying to mock a static method

StaticClass.staticMethod(id).setContentType("text/plain").build();

I was able to mock static method and its return type using PowerMockito as below :

PowerMockito.when(StaticClass.staticMethod(id)).thenReturn(returnValue);

But How do I send that value to the chained method setContentType()?

Upvotes: 0

Views: 996

Answers (1)

DCTID
DCTID

Reputation: 1337

You need multiple whens. You'll need to mock a couple more objects staticMethod and contentType.

PowerMockito.when(StaticClass.staticMethod(id)).thenReturn(staticMethod);
PowerMockito.when(staticMethod.setContentType("text/plain")).thenReturn(contentType);
PowerMockito.when(contentType.build()).thenReturn(returnValue);

Upvotes: 2

Related Questions