Added a test with multiple contracts in a file

This commit is contained in:
Marcin Grzejszczak
2016-12-07 17:26:07 +01:00
parent 84d361bae7
commit 06243abe3c
5 changed files with 84 additions and 7 deletions

View File

@@ -41,6 +41,7 @@
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
<version>1.1.0.BUILD-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<!-- end::stub_runner[] -->

View File

@@ -15,6 +15,7 @@ import com.example.loan.model.FraudServiceResponse;
import com.example.loan.model.LoanApplication;
import com.example.loan.model.LoanApplicationResult;
import com.example.loan.model.LoanApplicationStatus;
import com.example.loan.model.Response;
@Service
public class LoanApplicationService {
@@ -67,6 +68,26 @@ public class LoanApplicationService {
return new LoanApplicationResult(applicationStatus, response.getRejectionReason());
}
public int countAllFrauds() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE, FRAUD_SERVICE_JSON_VERSION_1);
ResponseEntity<Response> response =
restTemplate.exchange("http://localhost:" + port + "/frauds", HttpMethod.GET,
new HttpEntity<>(httpHeaders),
Response.class);
return response.getBody().getCount();
}
public int countDrunks() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_TYPE, FRAUD_SERVICE_JSON_VERSION_1);
ResponseEntity<Response> response =
restTemplate.exchange("http://localhost:" + port + "/drunks", HttpMethod.GET,
new HttpEntity<>(httpHeaders),
Response.class);
return response.getBody().getCount();
}
public void setPort(int port) {
this.port = port;
}

View File

@@ -0,0 +1,20 @@
package com.example.loan.model;
public class Response {
private int count;
public Response(int count) {
this.count = count;
}
public Response() {
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
}

View File

@@ -1,7 +1,5 @@
package com.example.loan;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,6 +14,8 @@ import com.example.loan.model.LoanApplication;
import com.example.loan.model.LoanApplicationResult;
import com.example.loan.model.LoanApplicationStatus;
import static org.assertj.core.api.Assertions.assertThat;
// tag::autoconfigure_stubrunner[]
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.NONE)
@@ -55,4 +55,20 @@ public class LoanApplicationServiceTests {
}
// end::client_tdd[]
@Test
public void shouldSuccessfullyGetAllFrauds() {
// when:
int count = service.countAllFrauds();
// then:
assertThat(count).isEqualTo(200);
}
@Test
public void shouldSuccessfullyGetAllDrunks() {
// when:
int count = service.countDrunks();
// then:
assertThat(count).isEqualTo(100);
}
}

View File

@@ -17,17 +17,16 @@ public class FraudStatsController {
@GetMapping(
value = "/frauds",
produces = FRAUD_SERVICE_JSON_VERSION_1)
public int countAllFrauds() {
return this.statsProvider.count(FraudType.ALL);
public Response countAllFrauds() {
return new Response(this.statsProvider.count(FraudType.ALL));
}
@GetMapping(
value = "/drunks",
produces = FRAUD_SERVICE_JSON_VERSION_1)
public int countAllDrunks() {
return this.statsProvider.count(FraudType.DRUNKS);
public Response countAllDrunks() {
return new Response(this.statsProvider.count(FraudType.DRUNKS));
}
}
enum FraudType {
@@ -37,3 +36,23 @@ enum FraudType {
interface StatsProvider {
int count(FraudType fraudType);
}
class Response {
private int count;
public Response(int count) {
this.count = count;
}
public Response() {
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
}