Last active
October 5, 2019 11:22
-
-
Save fbiville/e96a50ed6b6d79d771714dd5522b7c26 to your computer and use it in GitHub Desktop.
Dynamic WireMock port in Spring Boot test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RunWith(SpringRunner.class) | |
@SpringBootTest | |
@ContextConfiguration(initializers = CustomContextInitializer.class) | |
public abstract class SomeTest { | |
// [...] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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