Added support for writing contracts in Java

fixes gh-1161
This commit is contained in:
Marcin Grzejszczak
2019-08-12 13:40:23 +02:00
parent e26eff962e
commit 706044bfe5
32 changed files with 1677 additions and 93 deletions

View File

@@ -24,6 +24,7 @@ import java.nio.file.Files;
import java.util.Arrays;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -60,6 +61,10 @@ public class TestController {
return new Test("ok");
}
@GetMapping("/example")
public void example() {
}
@PutMapping(value = "/1", consumes = MediaType.APPLICATION_OCTET_STREAM_VALUE, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public byte[] response(@RequestBody byte[] requestBody) {
if (!Arrays.equals(this.request, requestBody)) {

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.fraud;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
public class TestBase {
@Before
public void setUp() throws Exception {
RestAssuredMockMvc.standaloneSetup(new TestController());
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2013-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test;
import java.util.function.Supplier;
import org.springframework.cloud.contract.spec.Contract;
class MyHttpContract implements Supplier<Contract> {
@Override
public Contract get() {
return Contract.make(c -> {
c.request(r -> {
r.method(r.GET());
r.url("/example");
});
c.response(r -> {
r.status(r.OK());
});
});
}
}