1

I have one instance of wiremock that is used across multiple test classes, It has worked fine until recently, when used to test async methods, when the test classes are ran singly, tests pass but when the entire tests are ran(mvn test), some of the async class tests fail with ConditionTimeOut error. The verify is failing because, I presume, the wiremock server was not done when the verify was called and the awaitility library is waiting for it. Just my understanding based on this links --> https://github.com/tomakehurst/wiremock/issues/565

https://github.com/tomakehurst/wiremock/issues/574

here is my wiremock class def :

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 9099)
public class WireMockTest {
    @Autowired
    public wireMockClassA wireMockClassA;
    @Autowired
    public wireMockClassB wireMockClassB;

   //other definitions here and more wiremock class...
    
}

here is an example test async class:

public class SaleWireMockTest extends WireMockTest {

@Test
    void call_sale_endpoint_and_return_200() {
        wireMockClassA.callSaleEndpoint(PATH,  request, HttpStatus.OK);

        makeAsyncCall();
        await().atMost(1, TimeUnit.SECONDS).untilAsserted(() ->
                 wireMockClassA.verify(1, request));
    }

 //more test methods here....

}

stack:

  1. java 14
  2. wiremock 2.26.2
  3. Spring boot 2.3.2.RELEASE
lawDino
  • 11
  • 3
  • I think I'm a little confused about what your exact issue is. Based on your summary, you'd expect this error? What exactly are you asking for help with? Is it finding a way to ensure that the request has completed before making your assertion? – agoff Dec 21 '20 at 16:30
  • @agoff, so, I needed a way to wait for request to complete before making the assertion. Each time I try with a thread.sleep(), I get unpredictable behavior as a result of multiple threads using the same wiremock instance. – lawDino Dec 21 '20 at 22:58
  • What version of Java are you using? If you're using 11+, then you can use Java's HTTP client: https://stackoverflow.com/a/59219527/11625850 If you're using another version, you can probably get away with using Future Tasks or something similar - I'd recommend looking into making network calls asynchronously with Java. A few links from Baeldung on how to achieve this: https://www.baeldung.com/java-asynchronous-programming https://www.baeldung.com/java-future#1-implementing-futures-with-futuretask https://www.baeldung.com/java-completablefuture FWIW I've had success with Future Tasks. – agoff Dec 22 '20 at 14:26
  • @agoff, tried both java 11 client and springboot webflux client. None fixed it. not sure anymore what the problem could be. my guess had been that it is timing out because it is failing to wait for the wiremock server to return. – lawDino Dec 23 '20 at 20:06

1 Answers1

1

Have you tried setting the first poll time?

await().pollDelay(Duration.ofSeconds(1)).atMost(Duration.ofSeconds(3)).untilAsserted(() -> {...});
WesternGun
  • 11,303
  • 6
  • 88
  • 157