Skip to content

Instantly share code, notes, and snippets.

@tomakehurst
Created November 18, 2016 10:57
Show Gist options
  • Save tomakehurst/dd09638e631acf76e723c18fa52fb417 to your computer and use it in GitHub Desktop.
Save tomakehurst/dd09638e631acf76e723c18fa52fb417 to your computer and use it in GitHub Desktop.
package com.github.tomakehurst.wiremock;
import org.junit.Test;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class SpringRestTemplateTest extends AcceptanceTestBase {
ExecutorService executorService = Executors.newFixedThreadPool(200);
@Test
public void restTemplate() throws Exception {
wm.stubFor(put(urlEqualTo("/put-this"))
.withRequestBody(equalTo("things"))
.willReturn(aResponse().withStatus(200)));
final RestTemplate restTemplate = new RestTemplate();
List<Future<Integer>> futures = newArrayList();
for (int i = 0; i < 2000; i++) {
final int count = i;
futures.add(executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
RequestEntity<String> request =
new RequestEntity<>(
"things",
HttpMethod.PUT,
URI.create("http://localhost:" + wireMockServer.port() + "/put-this")
);
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
System.out.println("Sent " + count);
return response.getStatusCode().value();
}
}));
}
for (Future<Integer> future: futures) {
assertThat(future.get(), is(200));
}
}
}
@dair-targ
Copy link

What's in AcceptanceTestBase? Specifically, how is wm constructed?

@dair-targ
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment