1

I'm not an expert with Wiremock and probably there's something wrong in my settings/configuration but I'm facing a weird scenario when it comes to verify a specific test.

if I run this specific test among the whole IT suite, the test fails on WireMockServer.verify(int count, RequestPatternBuilder requestPatternBuilder) but if I run the test alone, it works fine and the assertion is correct.

so what I did so far to understand this weird behavior is :

  • I set a break point the check if the actualCount inside the method was correct and indeed it is (only if run alone).

  • more over, I actually put some log messages to verify on the application side that this method has been invoked 4 times (one is the first request which throws exception and the other 3 are the so called retries)

  • I've debugged this specific use case by sending some requests with postman and the behaviour is what I expected

so at the moment I got no clue about what the heck is going on with this specific test when is running among the whole IT suite.

any input about what could it be the issue or why Wiremock behaves in this specific way?

Thank you in advance for the help & inputs :)

Maxuel
  • 127
  • 10
  • Can you provide a code example of where it fails? What tests are you running? What do the stubs look like? – agoff Apr 05 '22 at 00:22

1 Answers1

1

I have the same issue: tests finish successfully if I run them separately and fail when run with a class. Wiremock version "com.github.tomakehurst:wiremock-jre8:2.33.1"

package com.kn.tic.web.client.jira;

import com.atlassian.jira.rest.client.api.domain.input.IssueInput;
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.api.domain.util.UriUtil;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.kn.tic.config.ApplicationProperties;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

class CustomAsynchronousIssueRestClientTest {

    private static final String USER = "user";
    private static final String PASS = "pass";
    private static final String HOST = "localhost";
    private static final String PROTOCOL = "http";
    private static final String JIRA_TRACKING_ID = "jiraTrackingId";

    @TempDir
    Path tempDir;

    private final WireMockServer wireMockServer = new WireMockServer();
    private JiraClient jiraClient;

    @BeforeEach
    public void setUp() {
        this.wireMockServer.start();

        ApplicationProperties applicationProperties = new ApplicationProperties();
        applicationProperties.getJira().setUsername(USER);
        applicationProperties.getJira().setPassword(PASS);
        applicationProperties.getJira().setHost(HOST);
        applicationProperties.getJira().setProtocol(PROTOCOL);
        applicationProperties.getJira().setPort(wireMockServer.port());

        this.jiraClient = new JiraClient(applicationProperties.getJira());
    }


    @AfterEach
    void afterAll() {
        this.wireMockServer.stop();
    }

    @Test
    void testThatCustomCreateIssueImplementationAddsJiraTrackingIdToQuery() {
        String jiraTrackingId = UUID.randomUUID().toString();
        String uriPath = "/rest/api/latest/issue";

        wireMockServer.stubFor(post(urlPathEqualTo(uriPath))
            .withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
            .withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE))
            .withBasicAuth(USER, PASS)
            .willReturn(
                aResponse()
                    .withStatus(HttpStatus.SC_OK)
                    .withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .withBody("{\"id\":\"2197656\",\"key\":\"TTT-822\",\"self\":\"https://test-jira.com/rest/api/latest/issue/2197656\"}")
            ));

        IssueInput issueInput = new IssueInputBuilder()
            .setProjectKey("TCF")
            .setIssueTypeId(5L)
            .setSummary("summary")
            .setReporterName("reporter")
            .setDescription("description")
            .build();

        jiraClient.createIssue(issueInput, jiraTrackingId);

        verify(postRequestedFor(urlPathEqualTo(uriPath))
            .withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
            .withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.APPLICATION_JSON_VALUE)));
    }

    @Test
    void testThatCustomAddAttachmentImplementationAddsJiraTrackingIdToQuery() throws URISyntaxException, IOException {

        String jiraTrackingId = UUID.randomUUID().toString();
        URI issueUri = new URI(String.format("%s://%s:%s/rest/api/latest/issue/2197654",PROTOCOL, HOST, wireMockServer.port()));
        URI attachmentUri = UriUtil.path(issueUri, "attachments");
        wireMockServer.stubFor(post(urlPathEqualTo(attachmentUri.getPath()))
            .withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
            .withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.MULTIPART_FORM_DATA_VALUE))
            .withBasicAuth(USER, PASS)
            .willReturn(
                aResponse()
                    .withStatus(HttpStatus.SC_OK)
            ));

        Path tempFile = Files.createFile(tempDir.resolve("attachment.pdf"));
        Files.writeString(tempFile, "test");
        List<Pair<File, String>> attachments = List.of(Pair.of(tempFile.toFile(), "attachment.pdf"));
        jiraClient.addAttachmentsToIssue(issueUri, attachments, jiraTrackingId);

        verify(postRequestedFor(urlPathEqualTo(attachmentUri.getPath()))
            .withQueryParam(JIRA_TRACKING_ID, equalTo(jiraTrackingId))
            .withHeader(HttpHeaders.CONTENT_TYPE, containing(MediaType.MULTIPART_FORM_DATA_VALUE)));
    }
}

user6419217
  • 59
  • 10
  • 1
    Ok. My colleague found the issue - I was using static ```verify``` method while I should have been using my server instance. Then I found a different issue - Wiremock can fail if your http requst returns Promise - in this case some things happen too fast and request is not registered within Wiremock. – user6419217 Apr 13 '22 at 14:19
  • Interesting , in my case I solved by sleeping the thread at server configuration (not the cleanest solution) – Maxuel Apr 14 '22 at 19:09