Last active
September 1, 2023 20:16
-
-
Save up1/579a527696a6ec0e86f08cd90dffd3d5 to your computer and use it in GitHub Desktop.
Spring Cloud Contract
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
@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); | |
} | |
} |
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
@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"); | |
} | |
} |
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
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