How to correctly unit test RestEasy answers?

I have a RESTEasy client endpoint that connects to a REST database server that gives JSON responses. The other end data model (POJO annotated with JSON annotations) is available for use.

I would like to correctly unit test methods that communicate with requests / responses. Test code written with JUnit and Mockito.

The image becomes more complex in the case of the method that sends the request, needs to analyze the JSON result, must extract a new identifier for the created object and "reuse" this identifier to make another request to replace the automatically generated device name with a more readable and friendlier name. I thought that this method should be divided into two methods, but I don’t see any clean and good solution, because this method will be used as an access point from other components of the system, so one method should do one thing.

Suffice it to say, let's see the code itself:

public void createDevice(final Device device) {
    final RequestDescriptor requestDescriptor = new DeviceCreateRequestDescriptor(device);
    final RequestCreator requestCreator = new RequestCreator(requestDescriptor, HttpMethod.POST, uriBuilder);
    final ClientRequest request = requestCreator.create();
    final ClientResponse response = executor.execute(request);
    final String responseString = (String) response.getEntity(String.class);

    //JSON unmarshall logic will be here
    //I should get the device ID from the response, doable with e.g. Jackson
    String deviceId = "";

    changeDeviceName(device, deviceId);
}

   private void changeDeviceName(final Device device, final String deviceId) throws Exception {
    final UriBuilderWrapper uriBuilder = new UriBuilderWrapper(databaseConfig,
            m2mInterfaceContainer.getM2MProvisioningInterface());
    final RequestDescriptor requestDescriptor = new DeviceNameChangeRequestDescriptor(device, deviceId);
    final RequestCreator requestCreator = new RequestCreator(requestDescriptor, HttpMethod.POST, uriBuilder);
    final ClientRequest request = requestCreator.create();
    executor.execute(request);
}

My test code looks like this:

@Test
public void testCreateDeviceSendsTwoRequests() throws Exception {
    clientExecutorWrapper = mock(ClientExecutorWrapper.class);

    m2mDeviceManager = new M2MDeviceManager();
    device = new Device("123456666", "6789066666");
    m2mDeviceManager.createDevice(device);

    final ArgumentCaptor<ClientRequest> requestCaptor = ArgumentCaptor.forClass(ClientRequest.class);
    verify(clientExecutorWrapper, times(2)).execute(requestCaptor.capture());

    final List<ClientRequest> capturedRequests = requestCaptor.getAllValues();

    final Map<String, String> expectedFormParametersForCreateDeviceRequest = createExpectedFormParametersForFirstRequest();
    final Map<String, String> expectedFormParametersForChangeNameRequest = createExpectedFormParametersForSecondRequest();

    final String expectedUrlCreateDeviceRequest = "dummy_url_1;
    final String expectedUrlForChangeNameRequest = "dummy_url_2";
    RequestChecker.checkRequest(capturedRequests.get(0), expectedFormParametersForCreateDeviceRequest,
            expectedUrlCreateDeviceRequest);
    RequestChecker.checkRequest(capturedRequests.get(1), expectedFormParametersForChangeNameRequest,
            expectedUrlForChangeNameRequest);
}

My helper methods for the test case are as follows:

private Map<String, String> createExpectedFormParametersForFirstRequest() {
    final Map<String, String> expectedFormParameters = new HashMap<String, String>();
    expectedFormParameters.put("some_param","some_value");
    // ... other form parameters
    return expectedFormParameters;
}

private Map<String, String> createExpectedFormParametersForSecondRequest() {
    final Map<String, String> expectedFormParameters = new HashMap<String, String>();
    expectedFormParameters.put("some_param2","some_value2");
    // ... other form parameters
    return expectedFormParameters;
}

The helper class RequestCheckerfor the test looks like this:

class RequestChecker {

public static void checkRequest(final ClientRequest request, final Map<String, String> expectedFormParameters,
        final String expectedUrl) throws Exception {
    final MultivaluedMap<String, String> formParameters = request.getFormParameters();
    final MultivaluedMap<String, String> queryParameters = request.getQueryParameters();

    assertEquals(expectedUrl, request.getUri());

    assertTrue(queryParameters.size() == 0);
    checkFormParameters(expectedFormParameters, formParameters);

 }

private static void checkFormParameters(final Map<String, String> expectedFormParameters,
        final MultivaluedMap<String, String> formParameters) {
    final Set<Entry<String, String>> expectedParameters = expectedFormParameters.entrySet();
    for (final Entry<String, String> expectedParameter : expectedParameters) {
        final String expectedParameterName = expectedParameter.getKey();
        final String expectedParameterValue = expectedParameter.getValue();
        final List<String> actualFormParameters = formParameters.get(expectedParameterName);
        checkFormParameter(actualFormParameters, expectedParameterValue);
    }
}

 private static void checkFormParameter(final List<String> actualFormParameters, final String expected) {
    assertNotNull(actualFormParameters);
    assertTrue(actualFormParameters.size() == 1);
    assertEquals(expected, actualFormParameters.get(0));
 }
}

, unit test - :

executor ( ), JSON . , , JSON, . , , , . , changeDeviceName .

:

  • , , unmarshalling, , ?

  • , , ? .

  • - . , - , , , , , .

+4
2

, , :

, , url body. JSON .

0

resteasy, restassured, .

0

All Articles