Skip to content

Instantly share code, notes, and snippets.

@skuzzle
Last active May 3, 2021 11:27
Show Gist options
  • Save skuzzle/f8130330548f2184106cb7003a762405 to your computer and use it in GitHub Desktop.
Save skuzzle/f8130330548f2184106cb7003a762405 to your computer and use it in GitHub Desktop.
Wiremock property injection for SpringBootTests
@SpringBootTest
@WithWiremock(injectHostInto = "your.application.property.serviceUrl")
public class ExampleTest {
@Autowired
private WiremockServcer wiremock;
@Value("${your.application.property.serviceUrl}")
private String serviceUrl;
@Test
void testWiremock() {
wiremock.stubFor(...):
WebClient.create(serviceUrl).post()....
wiremock.verify(...);
}
}
import java.util.Map;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.test.context.event.AfterTestExecutionEvent;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.google.common.base.Preconditions;
class WiremockInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private static final String INJECT_PROPERTY = "wiremock.injectHostInto";
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
final WireMockServer wiremockServer = startWiremock();
injectWiremockHostIntoProperty(configurableApplicationContext, wiremockServer);
registerWiremockServerAsBean(configurableApplicationContext, wiremockServer);
addLifecycleEvents(configurableApplicationContext, wiremockServer);
}
private WireMockServer startWiremock() {
final WireMockServer wiremockServer = new WireMockServer(new WireMockConfiguration()
.dynamicPort());
wiremockServer.start();
return wiremockServer;
}
private void injectWiremockHostIntoProperty(ConfigurableApplicationContext applicationContext,
WireMockServer wiremockServer) {
final String injectPropertyName = applicationContext.getEnvironment()
.getProperty(INJECT_PROPERTY);
Preconditions.checkState(injectPropertyName != null,
"You need to define the property '%s' to hold the name of the "
+ "property into which the wiremock host will be injected",
INJECT_PROPERTY);
TestPropertyValues
.of(Map.of(injectPropertyName, wiremockServer.baseUrl()))
.applyTo(applicationContext);
}
private void registerWiremockServerAsBean(ConfigurableApplicationContext applicationContext,
WireMockServer wiremockServer) {
applicationContext
.getBeanFactory()
.registerSingleton("wiremockServer", wiremockServer);
}
private void addLifecycleEvents(ConfigurableApplicationContext applicationContext,
WireMockServer wiremockServer) {
applicationContext.addApplicationListener(applicationEvent -> {
if (applicationEvent instanceof AfterTestExecutionEvent) {
wiremockServer.resetAll();
}
if (applicationEvent instanceof ContextClosedEvent) {
wiremockServer.stop();
}
});
}
}
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
import org.springframework.test.context.ContextConfiguration;
@Retention(RUNTIME)
@Target(TYPE)
@ContextConfiguration(initializers = WiremockInitializer.class)
@PropertyMapping("wiremock")
public @interface WithWiremock {
@PropertyMapping("injectHostInto")
String injectHostInto() default "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment