Brendon Dugan
02/17/2025, 11:52 AM@SpringBootTest
class TestControllerTest {
private WireMockServer wireMockServer;
private CloseableHttpClient httpClient = HttpClients.createDefault();
@BeforeEach
void setUp() {
wireMockServer = new WireMockServer(
WireMockConfiguration.options()
.port(8081)
);
wireMockServer.start();
WireMock.configureFor("localhost", 8081);
}
@AfterEach
void tearDown() {
wireMockServer.stop();
}
@Test
void testParam() throws Exception {
stubFor(
get(
"/test"
).andMatching(new AtiRequestMatcher(Map.of("Test", "test")))
.willReturn(
aResponse().withStatus(200).withBody("Successfully Mocked")
)
);
HttpGet getRequest = new HttpGet("<http://localhost:8081/test>");
CloseableHttpResponse httpResponse = httpClient.execute(getRequest);
assertEquals(200, httpResponse.getCode());
assertEquals("Successfully Mocked", EntityUtils.toString(httpResponse.getEntity()));
}
}
As well as the following RequestMatcher (note: This is just to test that I have things wired together correctly, which I apparently don't):
public class AtiRequestMatcher extends RequestMatcher {
private final Map<String, String> parameters;
public AtiRequestMatcher(Map<String, String> parameters) {
this.parameters = parameters;
}
@Override
public String getName() {
return "ati-request-matcher";
}
@Override
public MatchResult match(Request request) {
return MatchResult.of(true);
}
}
When I run this, I am getting the following error:
com.github.tomakehurst.wiremock.common.AdminException: Custom matchers can't be used when administering a remote WireMock server. Use WireMockRule.stubFor() or WireMockServer.stubFor() to administer the local instance.
For additional context, here are the dependencies I have on the project:
plugins {
id 'java'
id 'org.springframework.boot' version '3.4.2'
id 'io.spring.dependency-management' version '1.1.7'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'org.wiremock:wiremock-standalone:3.11.0'
testImplementation 'org.apache.httpcomponents.client5:httpclient5:5.4.2'
}
I'm at a loss for what I should be doing differently. Any suggestions?Tom
02/18/2025, 5:01 PMwireMockServer.stubFor(…)
which will directly configure the stub in the server and can thus attach a bit of custom code.Brendon Dugan
02/19/2025, 10:48 AMTom
02/19/2025, 10:49 AM