-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for nested tests annotated with JUnit 5's @nested
Fixes #10
- Loading branch information
1 parent
b10ab31
commit 65f9b2f
Showing
3 changed files
with
134 additions
and
25 deletions.
There are no files selected for viewing
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
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
80 changes: 80 additions & 0 deletions
80
wiremock-spring-boot/src/test/java/app/NestedClassWireMockSpringExtensionTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package app; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
import com.maciejwalkowiak.wiremock.spring.ConfigureWireMock; | ||
import com.maciejwalkowiak.wiremock.spring.EnableWireMock; | ||
import com.maciejwalkowiak.wiremock.spring.InjectWireMock; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.core.env.Environment; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SpringBootTest(classes = NestedClassWireMockSpringExtensionTest.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 NestedClassWireMockSpringExtensionTest { | ||
|
||
@SpringBootApplication | ||
static class AppConfiguration { | ||
} | ||
|
||
@Autowired | ||
private Environment environment; | ||
|
||
@InjectWireMock("todo-service") | ||
private WireMockServer topLevelClassTodoService; | ||
|
||
@Nested | ||
@DisplayName("Test Something") | ||
class NestedTest { | ||
|
||
@InjectWireMock("todo-service") | ||
private WireMockServer nestedClassTodoService; | ||
|
||
@Test | ||
void injectsWiremockServerToMethodParameter(@InjectWireMock("user-service") WireMockServer wireMockServer) { | ||
assertWireMockServer(wireMockServer, "user-service.url"); | ||
} | ||
|
||
@Test | ||
void injectsWiremockServerToNestedClassField() { | ||
assertWireMockServer(nestedClassTodoService, "todo-service.url"); | ||
} | ||
|
||
@Test | ||
void injectsWiremockServerToTopLevelClassField() { | ||
assertWireMockServer(topLevelClassTodoService, "todo-service.url"); | ||
} | ||
|
||
@Test | ||
void doesNotSetPropertyWhenNotProvided(@InjectWireMock("noproperty-service") WireMockServer wireMockServer) { | ||
assertThat(wireMockServer) | ||
.as("creates WireMock instance") | ||
.isNotNull(); | ||
} | ||
|
||
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()); | ||
} | ||
} | ||
} |