Генерирует тесты с помощью Spring Cloud Contract Verifier 14 Important Spring Cloud Contract Verifier’s purpose is NOT to start writing business features in the contracts. Assume that we have a business use case of fraud check. If a user can be a fraud for 100 different reasons, we would assume that you would create 2 contracts, one for the positive case and one for the negative case. Contract tests are used to test contracts between applications and not to simulate full behavior.
ADD DEPENDENCIES/PLUGINS org.springframework.cloud.contract.spec.Contract.make { description(""" Sends a positive verification message when person is eligible to get the beer ``` given: client is old enough when: he applies for a beer then: we'll send a message with a positive verification ``` """) // Label by means of which the output message can be triggered label 'accepted_verification' // input to the contract input { // the contract will be triggered by a method triggeredBy('clientIsOldEnough()') } // output message of the contract outputMessage { // destination to which the output message will be sent sentTo 'verifications' // the body of the output message body([ eligible: true ]) } } <dependency> <groupId>org.springframework.cloud </groupId> <artifactId>spring-cloud- starter-contract-verifier </artifactId> <scope>test</scope> </dependency> <plugin> <groupId>org.springframework.cloud </groupId> <artifactId>spring-cloud- contract-maven-plugin</artifactId> <version>${spring-cloud- contract.version}</version> <extensions>true</extensions> </plugin> ./MVNW CLEAN INSTALL GENERATED TESTS PROVIDE IMPLEMENTATION TO FIX THE TESTS FOR PRODUCER
зависимость Генерирует тесты и стабы Изменения контракта приведут к падениям тестов Не понятно как быть с контрактом для UI (JavaScript) Много примеров Groovy DSL or YAML Поддерживает Pact Broker
{ final RestTemplate call = new RestTemplate(); final User expectedResponse = new User(); expectedResponse.setUserName("Ivan"); expectedResponse.setFirstName("Ivan"); expectedResponse.setUserId("1"); final User forEntity = call.getForObject(provider.getConfig().url() + "/user/1", User.class); assertThat(forEntity, sameBeanAs(expectedResponse)); }
<scope>test</scope> </dependency> @RunWith(PactRunner.class) @Provider("test_provider" ) @PactBroker(host = "localhost", port = "80") @VerificationReports({"console", "markdown"}) public class TestPacts { ADD CODE FOR RUNNER ADD .JSON CONTRACT ADD TEST TARGET FOR PRODUCER private static ConfigurableApplicationContext application; @TestTarget public final Target target = new HttpTarget(8080); @BeforeClass public static void startSpring(){ application = SpringApplication.run(ProviderServiceApplication.class) } @State({"default", "extra"}) public void toDefaultState() { System.out.println("Now service in default state"); } ADD TEST WITH STATES WE WANT TO TEST
your pacts and verification results between projects, and make them useful for people too ▸ Solves the problem of how to share contracts and verification results between consumer and provider projects ▸ Automatically versions your contracts ▸ Allows you to visualize the relationships between your services ▸ … https://docs.pact.io/getting_started/sharing_pacts
testing. ▸ Functional testing of the provider - that is what the provider's own tests should do. Pact is about checking the contents and format of requests and responses. ▸ … 33 https://docs.pact.io/getting_started/what_is_pact_good_for
Consumer side with a different language on the Provider side, you must ensure you use a common Pact Specification between them or you will be unable to validate the Pacts. Понятно как быть с контрактом для UI (JavaScript) Поддерживает много языков Pact Broker решает вопрос с версионированием и где хранить контракты
string lastName = 2; required int32 age = 3; } Person.proto Person ivan = Person.newBuilder() .setAge(22) .setFirstName(“Ivan") .setLastName(“Petrushkin") .build(); Some java class 36
▸ Быстрее e2e (выгоднее) ▸ Заставляет тех, кто делает изменения Думать ▸ Как использовать Spring Cloud Contract ▸ Как использовать Pact ▸ Protobuf можно использовать как контракт 42