Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active October 5, 2019 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fbiville/e96a50ed6b6d79d771714dd5522b7c26 to your computer and use it in GitHub Desktop.
Save fbiville/e96a50ed6b6d79d771714dd5522b7c26 to your computer and use it in GitHub Desktop.
Dynamic WireMock port in Spring Boot test
import org.assertj.core.util.Maps;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import java.util.Map;
import static org.springframework.util.SocketUtils.findAvailableTcpPort;
public class CustomContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
int port = findAvailableTcpPort();
registerUrlProperty(configurableApplicationContext, port);
configurableApplicationContext.addBeanFactoryPostProcessor(new WireMockDefinitionPostProcessor(port));
}
private void registerUrlProperty(ConfigurableApplicationContext configurableApplicationContext, int port) {
Map<String, Object> map = Maps.newHashMap("my.customUrl", "http://localhost:" + port);
configurableApplicationContext.getEnvironment().getPropertySources().addFirst(new MapPropertySource("some-props", map));
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = CustomContextInitializer.class)
public abstract class SomeTest {
// [...]
}
import com.github.tomakehurst.wiremock.WireMockServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
class WireMockDefinitionPostProcessor implements BeanDefinitionRegistryPostProcessor {
private final ConstructorArgumentValues constructorArguments;
public WireMockDefinitionPostProcessor(int port) {
ConstructorArgumentValues constructorArguments = new ConstructorArgumentValues();
constructorArguments.addIndexedArgumentValue(0, port);
this.constructorArguments = constructorArguments;
}
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(WireMockServer.class);
beanDefinition.setConstructorArgumentValues(constructorArguments);
beanDefinition.setDestroyMethodName("shutdown");
beanDefinition.setInitMethodName("start");
registry.registerBeanDefinition("wiremock", beanDefinition);
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment