Monday, September 25, 2017

Mock super methods

I found a solution with PowerMock.
Unfortunately the design is not the best, the clients call one method of the abstract super class.
But we dont want to test the method of the super class one hundred times.

@RunWith(PowerMockRunner.class)
@PrepareForTest({Super.class})
public class ClientTest{

private Client client= new Client(createMock(RestClientBuilderProvider.class));

private static void mockGetResponse(final Response.Status status) {
   PowerMock.replace(PowerMock.method(Super.class, "getResponse", 
   String.class, String.class, Class.class, Map.class,
   Map.class)).with(new InvocationHandler() {
      @Override      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
         HTTPResponseWrapper response = new HTTPResponseWrapper();
         response.setStatusCode(status.getStatusCode());
         return response;
      }
   });
}

@Testpublic void testGetReferenceById() throws Exception {
   //given   mockGetResponse(NO_CONTENT);
   //when   HTTPResponseWrapper result = client.getPersonByReferenceId(ORG, REFERENCE_ID_VALUE);
   //then   assertEquals(NO_CONTENT.getStatusCode(), result.getStatusCode().intValue());
}

No comments:

Post a Comment