Skip to content

Commit

Permalink
Merge pull request #52 from wiremock/feature/issue-49-incorrect-searc…
Browse files Browse the repository at this point in the history
…h-for-mappings

fix: looking for mocks under directory before classpath (refs #49)
  • Loading branch information
tomasbjerre authored Oct 10, 2024
2 parents d6caf57 + bb9ee82 commit 7f9bc1c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,29 @@ public WireMockServer createWireMockServer(

final WireMockConfiguration serverOptions =
options().port(serverPort).notifier(new Slf4jNotifier(options.name()));
this.configureFilesUnderClasspath(options.filesUnderClasspath(), "/" + options.name())

this.configureFilesUnderDirectory(options.filesUnderDirectory(), "/" + options.name())
.ifPresentOrElse(
present -> this.usingFilesUnderClasspath(serverOptions, present),
present -> this.usingFilesUnderDirectory(serverOptions, present),
() -> {
this.configureFilesUnderClasspath(options.filesUnderClasspath(), "")
this.configureFilesUnderDirectory(options.filesUnderDirectory(), "")
.ifPresentOrElse(
present -> this.usingFilesUnderClasspath(serverOptions, present),
present -> this.usingFilesUnderDirectory(serverOptions, present),
() -> {
this.configureFilesUnderDirectory(
options.filesUnderDirectory(), "/" + options.name())
this.logger.info("No mocks found under directory");
this.configureFilesUnderClasspath(
options.filesUnderClasspath(), "/" + options.name())
.ifPresentOrElse(
present -> this.usingFilesUnderDirectory(serverOptions, present),
present -> this.usingFilesUnderClasspath(serverOptions, present),
() -> {
this.configureFilesUnderDirectory(
options.filesUnderDirectory(), "")
.ifPresent(
this.configureFilesUnderClasspath(
options.filesUnderClasspath(), "")
.ifPresentOrElse(
present ->
this.usingFilesUnderDirectory(
serverOptions, present));
this.usingFilesUnderClasspath(serverOptions, present),
() -> {
this.logger.info("No mocks found under classpath");
});
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "GET",
"url": "/wiremockmappingsmock"
"url": "/files-under-directory"
},
"response": {
"jsonBody": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,45 @@

import static org.assertj.core.api.Assertions.assertThat;

import app.todoclient.Todo;
import app.todoclient.TodoClient;
import java.util.List;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.wiremock.spring.ConfigureWireMock;
import org.wiremock.spring.EnableWireMock;

@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(
name = "todo-client",
baseUrlProperties = "todo-client.url",
filesUnderClasspath = "custom-location")
})
@EnableWireMock({@ConfigureWireMock(filesUnderClasspath = "custom-location")})
class FilesUnderClasspathTest {

@Autowired private TodoClient todoClient;
@Value("${wiremock.server.baseUrl}")
private String wireMockServerUrl;

@Test
void usesStubFilesFromCustomLocation() {
final List<Todo> todos = this.todoClient.findAll();
assertThat(todos).isNotNull().hasSize(2);
assertThat(todos.get(0))
.satisfies(
todo -> {
assertThat(todo.id()).isEqualTo(1);
assertThat(todo.title()).isEqualTo("custom location todo 1");
});
assertThat(todos.get(1))
.satisfies(
todo -> {
assertThat(todo.id()).isEqualTo(2);
assertThat(todo.title()).isEqualTo("custom location todo 2");
});
void test() {
RestAssured.baseURI = this.wireMockServerUrl;
final String actual =
RestAssured.when()
.get("/classpathmappings")
.then()
.statusCode(200)
.extract()
.asPrettyString();
assertThat(actual)
.isEqualToIgnoringWhitespace(
"""
[
{
"id": 1,
"title": "custom location todo 1",
"userId": 1
},
{
"id": 2,
"title": "custom location todo 2",
"userId": 1
}
]
""");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app;

import static org.assertj.core.api.Assertions.assertThat;

import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -8,12 +10,7 @@
import org.wiremock.spring.EnableWireMock;

@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(
name = "fs-client",
filesUnderClasspath = "does_not_exist",
filesUnderDirectory = "src/test/wiremock-mappings")
})
@EnableWireMock({@ConfigureWireMock(filesUnderDirectory = "src/test/files-under-directory")})
class FilesUnderDirectoryTests {

@Value("${wiremock.server.baseUrl}")
Expand All @@ -22,6 +19,16 @@ class FilesUnderDirectoryTests {
@Test
void test() {
RestAssured.baseURI = this.wireMockServerUrl;
RestAssured.when().get("/wiremockmappingsmock").then().statusCode(200);
final String actual =
RestAssured.when()
.get("/files-under-directory")
.then()
.statusCode(200)
.extract()
.asPrettyString();

assertThat(actual).isEqualToIgnoringWhitespace("""
{"wiremockmappingsmock":"yes"}
""");
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "GET",
"url": "/"
"url": "/classpathmappings"
},
"response": {
"headers": {
Expand Down

0 comments on commit 7f9bc1c

Please sign in to comment.