PDF
Contents1使Spring Cloud ContractContents西@GetMapping("/vehicle/{vin}")VehicleDetail getVehicleDetail(@PathVariable String vin){ VehicleDetail item = this.vehicleService.getVehicle(vin); if(item == null) throw new VehicleNotFoundException(); return item;}Contract.make { request { method 'GET' url value('/vehicle/WDC1660631A7506890') } response { status 200 body([ vin : 'WDC1660631A7506890', brand : 'Audi X5', owner : 'James ', registeredDate: 1502347667000, mileage : 1200 ]) headers { header('Content-Type': value( producer(regex('application/json.*')), consumer('application/json') Contents2 )) } }}generateContractTests@Beforepublic void setUp() throws Exception { VehicleDetail i = new VehicleDetail(); i.setVin("WDC1660631A7506890"); i.setOwner("James "); i.setBrand("Audi X5"); i.setRegisteredDate(new Date(1502347667000L)); i.setMileage(1200); RestAssuredMockMvc.webAppContextSetup(context); given(vehicleService.getVehicle("WDC1660631A7506890")).willReturn(i);}Contract.make { request { method 'GET' url value(consumer(regex('/vehicle/[A-Z0-9]{18}')), producer('/vehicle/WDC1660631A7506890')) } response { status 200 body([ vin : $(producer(regex(/[A-Z0-9]{18}/))), brand : $(producer(anyNonBlankString())), owner : $(producer(anyNonBlankString())), registeredDate: $(producer(regex(/[1-9][0-9]{11,12}/))), mileage : $(producer(regex(/[1-9][0-9]{0,10}/))) ]) headers { header('Content-Type': value( producer(regex('application/json.*')), consumer('application/json') )) } }}Contract.make { request { method 'GET' Contents3 url value(consumer(regex('/vehicle/\\w.+')), producer('/vehicle/XXXXX')) } response { status 404 }}install@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureStubRunner(ids = "com.riguz:foo:+:stubs:10000", workOffline = true)public class CustomerServiceTest { @Autowired private CustomerService customerService; @Test public void shouldReturnCustomerDetail(){ CustomerInfo info = this.customerService.getCustomerInfo("123"); System.out.println(info); assertEquals(1, info.getVehicles().size()); // ... }}~/.m2/repository/com/riguz/foo/1.0-SNAPSHOT/foo-1.0-SNAPSHOT-stubs.jar+10000generateClientStubsbuild/stubs/....{ "id" : "5dd47b81-a184-4b9e-be02-b6e22c409c81", "request" : { "url" : "/vehicle/WDC1660631A7506890", "method" : "GET" }, "response" : { "status" : 200, "body" : "{\"vin\":\"WDC1660631A7506890\",\"brand\":\"Audi X5\",\"owner\":\"James \\u738b\",\"registeredDate\":1502347667000,\"mileage\":1200}", "headers" : { "Content-Type" : "application/json" Contents4 }, "transformers" : [ "response-template" ] }, "uuid" : "5dd47b81-a184-4b9e-be02-b6e22c409c81"}java -jar wiremock-standalone-2.7.1.jar# mappingsmappings访http://localhost:8080/vehicle/WDC1660631A7506890使西

HTML view coming soon.

Download PDF for the full formatted version.