Skip to content

Commit

Permalink
polish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejwalkowiak committed Mar 29, 2024
1 parent 08d4043 commit 4a85af0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.maciejwalkowiak.wiremock.spring.ConfigureWireMock;
import com.maciejwalkowiak.wiremock.spring.EnableWireMock;
import com.maciejwalkowiak.wiremock.spring.InjectWireMock;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,54 +14,76 @@

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

@SpringBootTest(classes = WireMockSpringExtensionTest.AppConfiguration.class)
@EnableWireMock({
@ConfigureWireMock(name = "user-service", property = "user-service.url"),
@ConfigureWireMock(name = "todo-service", property = "todo-service.url"),
@ConfigureWireMock(name = "noproperty-service")
})
public class WireMockSpringExtensionTest {

@SpringBootApplication
static class AppConfiguration {
@SpringBootTest(classes = WireMockSpringExtensionTest.AppConfiguration.class)
@EnableWireMock({
@ConfigureWireMock(name = "user-service", property = "user-service.url"),
@ConfigureWireMock(name = "todo-service", property = "todo-service.url"),
@ConfigureWireMock(name = "noproperty-service")
})
@Nested
class SinglePropertyBindingTest {

}
@InjectWireMock("todo-service")
private WireMockServer todoWireMockServer;

@InjectWireMock("todo-service")
private WireMockServer todoWireMockServer;
@Autowired
private Environment environment;

@Autowired
private Environment environment;
@Test
void createsWiremockWithClassLevelConfigureWiremock(@InjectWireMock("user-service") WireMockServer wireMockServer) {
assertWireMockServer(wireMockServer, "user-service.url");
}

@Test
void createsWiremockWithClassLevelConfigureWiremock(@InjectWireMock("user-service") WireMockServer wireMockServer) {
assertWireMockServer(wireMockServer, "user-service.url");
}
@Test
void createsWiremockWithFieldLevelConfigureWiremock() {
assertWireMockServer(todoWireMockServer, "todo-service.url");
}

@Test
void doesNotSetPropertyWhenNotProvided(@InjectWireMock("noproperty-service") WireMockServer wireMockServer) {
assertThat(wireMockServer)
.as("inject wiremock sets null when not configured")
.isNotNull();
}

@Test
void createsWiremockWithFieldLevelConfigureWiremock() {
assertWireMockServer(todoWireMockServer, "todo-service.url");
private void assertWireMockServer(WireMockServer wireMockServer, String property) {
assertThat(wireMockServer)
.as("creates WireMock instance")
.isNotNull();
assertThat(wireMockServer.baseUrl())
.as("WireMock baseUrl is set")
.isNotNull();
assertThat(wireMockServer.port())
.as("sets random port")
.isNotZero();
assertThat(environment.getProperty(property))
.as("sets Spring property")
.isEqualTo(wireMockServer.baseUrl());
}
}

@Test
void doesNotSetPropertyWhenNotProvided(@InjectWireMock("noproperty-service") WireMockServer wireMockServer) {
assertThat(wireMockServer)
.as("creates WireMock instance")
.isNotNull();
@SpringBootTest(classes = WireMockSpringExtensionTest.AppConfiguration.class)
@EnableWireMock(@ConfigureWireMock(name = "user-service", property = {"user-service.url", "todo-service.url"}))
@Nested
class MultiplePropertiesBindingTest {

@InjectWireMock("user-service")
private WireMockServer userServiceWireMockServer;

@Autowired
private Environment environment;

@Test
void bindsUrlToMultipleProperties() {
assertThat(environment.getProperty("user-service.url")).isEqualTo(userServiceWireMockServer.baseUrl());
assertThat(environment.getProperty("todo-service.url")).isEqualTo(userServiceWireMockServer.baseUrl());
}
}

private void assertWireMockServer(WireMockServer wireMockServer, String property) {
assertThat(wireMockServer)
.as("creates WireMock instance")
.isNotNull();
assertThat(wireMockServer.baseUrl())
.as("WireMock baseUrl is set")
.isNotNull();
assertThat(wireMockServer.port())
.as("sets random port")
.isNotZero();
assertThat(environment.getProperty(property))
.as("sets Spring property")
.isEqualTo(wireMockServer.baseUrl());
@SpringBootApplication
static class AppConfiguration {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

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 @@ -34,7 +31,7 @@
),
@ConfigureWireMock(
name = "todo-service",
property = {"todo-service.url", "bar-service.url"},
property = "todo-service.url",
configurationCustomizers = WireMockConfigurationCustomizerTest.SampleConfigurationCustomizer.class
),
})
Expand Down Expand Up @@ -66,15 +63,6 @@ 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

0 comments on commit 4a85af0

Please sign in to comment.