Skip to content

Commit

Permalink
feat: enable mappings outside classpath (refs maciejwalkowiak#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Apr 29, 2024
1 parent ddcc8fd commit f8ffec2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
32 changes: 32 additions & 0 deletions example/src/test/java/app/StubsInDirectoryTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.maciejwalkowiak.wiremock.spring.ConfigureWireMock;
import com.maciejwalkowiak.wiremock.spring.EnableWireMock;
import com.maciejwalkowiak.wiremock.spring.InjectWireMock;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
@EnableWireMock({
@ConfigureWireMock(name = "user-client", property = "user-client.url", stubLocation = "src/test/wiremock-mappings/user-client", stubLocationOnClasspath = false)
})
class StubsInDirectoryTests {

@Autowired
private UserClient userClient;

@InjectWireMock("user-client")
private WireMockServer wiremock;

@Test
void usesStubFiles() {
User user = userClient.findOne(1L);
assertThat(user).isNotNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"request": {
"method": "GET",
"url": "/1"
},
"response": {
"status": 200,
"jsonBody": { "id": 1, "name": "Jenna" },
"headers": {
"Content-Type": "application/json"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
*/
String stubLocation() default "";

/**
* Allows user to specify if the mappings should be loaded from classpath or a directory. The location is specified with {@link #stubLocation()}.
*
* @return true if stubLocation points to classpath directory, else it is an ordinary directory
*/
boolean stubLocationOnClasspath() default true;

/**
* WireMock extensions to register in {@link WireMockServer}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex
if (wireMockServer == null) {
// create & start wiremock server
WireMockConfiguration serverOptions = options()
.usingFilesUnderClasspath(resolveStubLocation(options))
.port(options.port())
.notifier(new Slf4jNotifier(true));
if (options.stubLocationOnClasspath()) {
serverOptions
.usingFilesUnderClasspath(resolveStubLocation(options));
} else {
serverOptions
.usingFilesUnderDirectory(options.stubLocation());
}

if (options.extensions().length > 0) {
serverOptions.extensions(options.extensions());
Expand Down

0 comments on commit f8ffec2

Please sign in to comment.