Fork me on GitHub

More Complex Plugin Configuration

Sample more complex configuration for Java Project with JUnit tests.

Project configuration for Spring Cloud Contract Verifier with JUnit tests and stub publishing

            <plugin>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-contract-maven-plugin</artifactId>
                <version>${spring-cloud-verifier-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>convert</goal>
                            <goal>generateStubs</goal>
                            <goal>generateTests</goal>
                        </goals>
                        <configuration>
                            <contractsDirectory>src/test/contracts</contractsDirectory>
                            <basePackageForTests>com.blogspot.toomuchcoding.frauddetection</basePackageForTests>
                            <testMode>MOCKMVC</testMode>
                            <testFramework>JUNIT</testFramework>
                            <classifier>stubs</classifier>
                            <nameSuffixForTests>Test</nameSuffixForTests>
                            <ruleClassForTests>org.junit.rules.ErrorCollector</ruleClassForTests>
                            <staticImports>
                                <staticImport>com.blogspot.toomuchcoding.frauddetection.matchers.CustomMatchers.*</staticImport>
                            </staticImports>
                            <imports>
                                <import>com.blogspot.toomuchcoding.frauddetection.matchers.CustomMatchers</import>
                            </imports>
                            <ignoredFiles>
                                <ignoredFile>broken**</ignoredFile>
                            </ignoredFiles>
                            <excludedFiles>
                                <param>shouldMarkClientAsFraud.groovy</param>
                            </excludedFiles>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <baseClassForTests>com.blogspot.toomuchcoding.frauddetection.BaseAccurest</baseClassForTests>
                </configuration>
            </plugin>

Base Test class

/**
 *
 *  Copyright 2013-2017 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.blogspot.toomuchcoding.frauddetection;

import io.restassured.module.mockmvc.RestAssuredMockMvc;

import org.junit.Before;

public class BaseAccurest {

        @Before
        public void setup() {
                RestAssuredMockMvc.standaloneSetup(new FraudDetectionController());
        }

}

Sample additional matcher

/**
 *
 *  Copyright 2013-2017 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.blogspot.toomuchcoding.frauddetection.matchers;

import org.junit.Assert;

public class CustomMatchers {

        public static void assertThatRejectionReasonIsNull(String rejectionReason) {
                Assert.assertNull(rejectionReason);
        }

}

Sample contract using matcher

/**
 *
 *  Copyright 2013-2017 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.
 */
org.springframework.cloud.contract.spec.Contract.make {
                                request {
                                        method 'PUT'
                                        url '/fraudcheck'
                                        body("""
                                                {
                                                "clientPesel":"${value(consumer(regex('[0-9]{10}')), producer('1234567890'))}",
                                                "loanAmount":123.123
                                                }
                                        """
                                        )
                                        headers {
                                                header('Content-Type', 'application/vnd.fraud.v1+json')
                                        }

                                }
                        response {
                                status OK()
                                body(
                                                fraudCheckStatus: "OK",
                                                rejectionReason: $(consumer(null), producer(execute('assertThatRejectionReasonIsNull($it)')))
                                )
                                headers {
                                         header('Content-Type': 'application/vnd.fraud.v1+json')
                                }
                        }

}

More samples

You can check out the Spring Cloud Contract Samples project for more examples of Maven plugin setup.