Skip to content

Instantly share code, notes, and snippets.

@up1
Last active September 1, 2023 20:16
Show Gist options
  • Save up1/579a527696a6ec0e86f08cd90dffd3d5 to your computer and use it in GitHub Desktop.
Save up1/579a527696a6ec0e86f08cd90dffd3d5 to your computer and use it in GitHub Desktop.
Spring Cloud Contract
@SpringBootTest (classes = ProviderApplication.class)
class BaseTests {
@Autowired
private CustomerRestController customerRestController;
@MockBean
private CustomerRepository customerRepository;
@BeforeEach
public void before() {
when(this.customerRepository.findAll())
.thenReturn(Arrays.asList(new Customer(1L, "Test 01"), new Customer(2L, "Test 02")));
RestAssuredMockMvc.standaloneSetup(this.customerRestController);
}
}
@SpringBootTest(webEnvironment= WebEnvironment.NONE)
@AutoConfigureStubRunner(ids = "com.example:provider:+:8081", stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class CustomerGatewayTest {
@Autowired
private CustomerGateway gateway;
@Test
void getAllCustomers() {
List<Customer> customers = this.gateway.getAllCustomers();
BDDAssertions.then(customers).size().isEqualTo(2);
BDDAssertions.then(customers.iterator().next().getId()).isEqualTo(1L);
BDDAssertions.then(customers.iterator().next().getName()).isEqualTo("Test 01");
}
}
import org.springframework.cloud.contract.spec.Contract
import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType
Contract.make {
description "should return all customers"
request {
url "/customers"
method GET()
}
response {
status 200
headers {
header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
}
body([[id: 1L, name: "Test 01"], [id: 2L, name: "Test 02"]])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment