Expose WireMockServer for @Autowired in test class

This commit is contained in:
Dave Syer
2016-07-26 10:44:20 +01:00
parent 44eebcab0f
commit 4305f40fd9
9 changed files with 75 additions and 62 deletions

View File

@@ -53,6 +53,10 @@
</dependency>
<!-- end::stub_runner[] -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
<!-- tag::contract_bom[] -->
<dependencyManagement>

View File

@@ -2,34 +2,46 @@ package com.example.loan;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.charset.Charset;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.cloud.contract.wiremock.WireMockSpring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils;
import com.example.loan.model.Client;
import com.example.loan.model.LoanApplication;
import com.example.loan.model.LoanApplicationResult;
import com.example.loan.model.LoanApplicationStatus;
import com.github.tomakehurst.wiremock.core.Options;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
@RunWith(SpringRunner.class)
@SpringBootTest(classes=TestConfiguration.class)
@SpringBootTest
@AutoConfigureWireMock
public class LoanApplicationServiceTests {
@Autowired
private LoanApplicationService sut;
@Value("classpath:META-INF/com.example/http-server-restdocs/0.0.1-SNAPSHOT/mappings/markClientAsFraud.json")
private Resource markClientAsFraud;
@Value("classpath:META-INF/com.example/http-server-restdocs/0.0.1-SNAPSHOT/mappings/markClientAsNotFraud.json")
private Resource markClientAsNotFraud;
@Autowired
private WireMockServer server;
@Test
public void shouldSuccessfullyApplyForLoan() {
public void shouldSuccessfullyApplyForLoan() throws Exception {
server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString(
markClientAsNotFraud.getInputStream(), Charset.forName("UTF-8"))));
// given:
LoanApplication application = new LoanApplication(new Client("1234567890"),
123.123);
@@ -42,7 +54,9 @@ public class LoanApplicationServiceTests {
}
@Test
public void shouldBeRejectedDueToAbnormalLoanAmount() {
public void shouldBeRejectedDueToAbnormalLoanAmount() throws Exception {
server.addStubMapping(StubMapping.buildFrom(StreamUtils.copyToString(
markClientAsFraud.getInputStream(), Charset.forName("UTF-8"))));
// given:
LoanApplication application = new LoanApplication(new Client("1234567890"),
99999);
@@ -54,14 +68,4 @@ public class LoanApplicationServiceTests {
assertThat(loanApplication.getRejectionReason()).isEqualTo("Amount too high");
}
@Configuration
@Import(Application.class)
protected static class TestConfiguration {
@Bean
public Options wiremockOptions() {
return WireMockSpring.options().usingFilesUnderClasspath("META-INF/wiremock");
}
}
}

View File

@@ -24,7 +24,7 @@
</fileSet>
<fileSet>
<directory>${project.build.directory}/snippets/stubs</directory>
<outputDirectory>META-INF/wiremock/mappings/${project.artifactId}</outputDirectory>
<outputDirectory>META-INF/${project.groupId}/${project.artifactId}/${project.version}/mappings</outputDirectory>
<includes>
<include>**/*</include>
</includes>

View File

@@ -1,5 +1,8 @@
package com.example.fraud;
import static org.springframework.cloud.contract.wiremock.RestDocsContracts.verify;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import java.math.BigDecimal;
import org.junit.Test;
@@ -10,14 +13,11 @@ import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDoc
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.cloud.contract.wiremock.RestDocsContracts;
import org.springframework.cloud.contract.wiremock.WireMockSnippet;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.example.fraud.model.FraudCheck;
@@ -32,9 +32,6 @@ public class StubGeneratorTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private WireMockSnippet snippet;
private JacksonTester<FraudCheck> json;
@Test
@@ -45,11 +42,9 @@ public class StubGeneratorTests {
mockMvc.perform(MockMvcRequestBuilders.put("/fraudcheck")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.content(json.write(fraudCheck).getJson()))
.andExpect(MockMvcResultMatchers.jsonPath("$.fraudCheckStatus")
.value("FRAUD"))
.andExpect(MockMvcResultMatchers.jsonPath("$.rejectionReason")
.value("Amount too high"))
.andDo(RestDocsContracts.content(snippet).jsonPath("$.clientId")
.andExpect(jsonPath("$.fraudCheckStatus").value("FRAUD"))
.andExpect(jsonPath("$.rejectionReason").value("Amount too high"))
.andDo(verify().jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount > 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.contract("markClientAsFraud"));
@@ -59,15 +54,13 @@ public class StubGeneratorTests {
public void shouldMarkClientAsNotFraud() throws Exception {
FraudCheck fraudCheck = new FraudCheck();
fraudCheck.setClientId("1234567890");
fraudCheck.setLoanAmount(BigDecimal.valueOf(1));
fraudCheck.setLoanAmount(BigDecimal.valueOf(123.123));
mockMvc.perform(MockMvcRequestBuilders.put("/fraudcheck")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.content(json.write(fraudCheck).getJson()))
.andExpect(
MockMvcResultMatchers.jsonPath("$.fraudCheckStatus").value("OK"))
.andExpect(MockMvcResultMatchers.jsonPath("$.rejectionReason")
.doesNotExist())
.andDo(RestDocsContracts.content(snippet).jsonPath("$.clientId")
.andExpect(jsonPath("$.fraudCheckStatus").value("OK"))
.andExpect(jsonPath("$.rejectionReason").doesNotExist())
.andDo(verify().jsonPath("$.clientId")
.jsonPath("$[?(@.loanAmount <= 1000)]")
.contentType(MediaType.valueOf("application/vnd.fraud.v1+json"))
.contract("markClientAsNotFraud"));