Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-28: add support for multiple property injection #29

Merged
merged 13 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
String name();

/**
* The name of Spring property to inject the {@link WireMockServer#baseUrl()}
* Names of Spring properties to inject the {@link WireMockServer#baseUrl()}.
*
* @return the name of Spring property to inject the {@link WireMockServer#baseUrl()}
* @return names of Spring properties to inject the {@link WireMockServer#baseUrl()}.
*/
String property() default "";
String[] property() default "";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current implementation is fully backward compatible.

But its readability is not as good as it could be when renaming the attribute to properties, is it ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Add properties and mark property as deprecated. For the time being we will support both, later the property will be removed.


/**
* The location of WireMock stub files. By default, stubs are resolved from classpath location <code>wiremock-server-name/mappings/</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ private void resolveOrCreateWireMockServer(ConfigurableApplicationContext contex
});

// configure Spring environment property
if (StringUtils.isNotBlank(options.property())) {
String property = options.property() + "=" + newServer.baseUrl();
Arrays.stream(options.property()).forEach(propertyName -> {
String property = propertyName + "=" + newServer.baseUrl();
LOGGER.debug("Adding property '{}' to Spring application context", property);
TestPropertyValues.of(property).applyTo(context.getEnvironment());
}
});
} else {
LOGGER.info("WireMockServer with name '{}' is already configured", options.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.core.env.Environment;
import org.springframework.test.util.TestSocketUtils;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
Expand All @@ -31,7 +34,7 @@
),
@ConfigureWireMock(
name = "todo-service",
property = "todo-service.url",
property = {"todo-service.url", "bar-service.url"},
configurationCustomizers = WireMockConfigurationCustomizerTest.SampleConfigurationCustomizer.class
),
})
Expand Down Expand Up @@ -63,6 +66,15 @@ static class AppConfiguration {
@InjectWireMock("todo-service")
private WireMockServer todoService;

@Test
void appliesPropertyInjection(@Autowired Environment environment) {
// TODO: @Maciej is there a programmatic way for accessing the @ConfigureWireMock ?
Stream.of("todo-service.url", "bar-service.url").forEach(property ->
assertThat(environment.getProperty(property)).isEqualTo("http://localhost:" + TODO_SERVICE_PORT));
Stream.of("user-service.url").forEach(property ->
assertThat(environment.getProperty(property)).isEqualTo("http://localhost:" + USER_SERVICE_PORT));
}

@Test
void appliesConfigurationCustomizer() {
assertThat(userService.port()).isEqualTo(USER_SERVICE_PORT);
Expand Down
Loading