0

I have created a custom extension (Connector), which sends an HttpRequest (using org.mule.runtime.http.api.client.HttpClient and the related classes).

The extension's unit tests file contains the following test, to which I've added a simple Mockito mock to throw a TimeoutException when the HTTP request is being sent:

public class DemoOperationsTestCase extends MuleArtifactFunctionalTestCase {

  /**
   * Specifies the mule config xml with the flows that are going to be executed in the tests, this file lives in the test resources.
   */
  @Override
  protected String getConfigFile() {
    return "test-mule-config.xml";
  }

  @Test
  public void executeSayHiOperation() throws Exception {
      HttpClient httpClient = mock(HttpClient.class);
      HttpRequest httpRequest = mock(HttpRequest.class);
      when(httpClient.send(any(HttpRequest.class), anyInt(), anyBoolean(), any(HttpAuthentication.class))).thenThrow(new TimeoutException());
      String payloadValue = ((String) flowRunner("sayHiFlow").run()
                                      .getMessage()
                                      .getPayload()
                                      .getValue());
      assertThat(payloadValue, is("Hello Mariano Gonzalez!!!"));
  }
}

The test should fail because the function should throw a TimeoutException, it is what I want for now.

The code that is being tested is as follows (redacted for convenience):

HttpClient client = connection.getHttpClient();
HttpResponse httpResponse = null;
String response = "N/A";
HttpRequestBuilder builder = HttpRequest.builder();

try {
    httpResponse = client
            .send(builder
                    .addHeader("Authorization", authorization)
                    .method("POST")
                    .entity(new ByteArrayHttpEntity("Hello from Mule Connector!".getBytes()))
                    .uri(destinationUrl)
                    .build(),
                    0, false, null);
    response = IOUtils.toString(httpResponse.getEntity().getContent());
} catch (IOException e) {
    e.printStackTrace();
    throw new ModuleException(DemoError.NO_RESPONSE, new Exception("Failed to get response"));
} catch (TimeoutException e) {
    e.printStackTrace();
    throw new ModuleException(DemoError.NO_RESPONSE, new Exception("Connection timed out"));
}

But I always get the "Failed to get response" error message, which is what I get when I run the Connector with a nonexistent server, therefore the mock isn't working (it actually tries to send an HTTP request).

I am new to Java unit testing, so it might be a general mocking issue and not specific to MuleSoft - though I came across other questions (such as this one and this one), I tried the suggestions in the answers and the comments, but I get the same error. I even tried to use thenReturn instead of thenThrow, and I get the same error - so the mock isn't working.

Any idea why this is happening?

Bolchojeet
  • 455
  • 5
  • 14
  • While Mule is developed mostly in Java, it is not meant to be compatible with Java unit testing frameworks like JUnit. The recommended why to implement unit tests is using MuleSoft's own MUnit framework. – aled Oct 12 '22 at 14:41
  • Are you trying to develop a custom connector? – Harshank Bansal Oct 12 '22 at 16:40
  • @aled I went over the MUnit documentation, and it seems like it does not support the kind of mocks that I want (as I wrote in the question), but only mocks of entire message processors, or flows for Mule Apps. I want to mock only a call to a method (`httpClient.send`), is it possible with MUnit? Have I missed something? – Bolchojeet Oct 13 '22 at 11:26
  • @HarshankBansal Yes, but while the connector itself works well, the unit test doesn't. – Bolchojeet Oct 13 '22 at 11:27
  • I'm sorry, I missed that you were trying to unit test a connector. MUnit is for Mule applications, not for connectors. I don't have experience with Mockito. I'm not sure if it is related but you may want to check if it works well with Mule 4 multi threaded architecture based on the Reactor framework. – aled Oct 13 '22 at 15:01

0 Answers0