basePackageForTests can be configured automatically

without this change we always set the default value of the package with generated tests, unless a value overrides it.
with this change the flow for setting the generated tests package name will look like this:

- pick basePackageForTests
- if basePackageForTests wasn't set pick the package from baseClassForTests
- if baseClassForTests wasn't set pick packageWithBaseClasses
- if nothing got set pick the default org.springframework.cloud.contract.verifier.tests

The rationale behind this change is such that often in the base class you would like to have package scoped methods and the fact that the tests are always generated in the default location requires to make these methods public or always override this property.

fixes #260
This commit is contained in:
Marcin Grzejszczak
2017-08-24 12:08:02 +02:00
parent a1839450fb
commit 3b88fea8bd
7 changed files with 170 additions and 43 deletions

View File

@@ -118,3 +118,15 @@ By introducing a method `String registeredMappings()` in the public interface
have to implement that method too. It should return a `String` representing
all mappings available in a single `HttpServerStub`. Related to
https://github.com/spring-cloud/spring-cloud-contract/issues/355[issue 355].
==== New packages for generated tests
The flow for setting the generated tests package name will look like this:
- pick `basePackageForTests`
- if `basePackageForTests` wasn't set pick the package from `baseClassForTests`
- if `baseClassForTests` wasn't set pick `packageWithBaseClasses`
- if nothing got set pick the default `org.springframework.cloud.contract.verifier.tests` value
Related to
https://github.com/spring-cloud/spring-cloud-contract/issues/260[issue 260].

View File

@@ -174,7 +174,8 @@ contracts {
- **testMode** - defines mode for acceptance tests. By default MockMvc which is based on Spring's MockMvc. It can also be changed to **JaxRsClient** or to **Explicit** for real HTTP calls.
- **imports** - array with imports that should be included in generated tests (for example ['org.myorg.Matchers']). By default empty array []
- **staticImports** - array with static imports that should be included in generated tests(for example ['org.myorg.Matchers.*']). By default empty array []
- **basePackageForTests** - specifies base package for all generated tests. By default set to org.springframework.cloud.verifier.tests
- **basePackageForTests** - specifies base package for all generated tests. If not set the value will be picked from `baseClassForTests`'s package and from
`packageWithBaseClasses`. If neither of these are set then the value will be set to `org.springframework.cloud.contract.verifier.tests`
- **baseClassForTests** - base class for all generated tests. By default `spock.lang.Specification` if using Spock tests.
- **packageWithBaseClasses** - instead of providing a fixed value for base class you can provide a package where all the base classes lay. Takes precedence over **baseClassForTests**.
- **baseClassMappings** - explicitly map contract package to a FQN of a base class. Takes precedence over **packageWithBaseClasses** and **baseClassForTests**.
@@ -425,7 +426,8 @@ To change default configuration just add `configuration` section to plugin defin
====== Important configuration options
- **testMode** - defines mode for acceptance tests. By default `MockMvc` which is based on Spring's MockMvc. It can also be changed to `JaxRsClient` or to `Explicit` for real HTTP calls.
- **basePackageForTests** - specifies base package for all generated tests. By default set to `org.springframework.cloud.verifier.tests`.
- **basePackageForTests** - specifies base package for all generated tests. If not set the value will be picked from `baseClassForTests`'s package and from
`packageWithBaseClasses`. If neither of these are set then the value will be set to `org.springframework.cloud.contract.verifier.tests`
- **ruleClassForTests** - specifies Rule which should be added to generated test classes.
- **baseClassForTests** - base class for generated tests. By default `spock.lang.Specification` if using Spock tests.
- **contractsDirectory** - directory containing contracts written using the GroovyDSL. By default `/src/test/resources/contracts`.

View File

@@ -59,7 +59,7 @@ public class GenerateTestsMojo extends AbstractMojo {
defaultValue = "${project.build.directory}/generated-test-sources/contracts")
private File generatedTestSourcesDir;
@Parameter(defaultValue = "org.springframework.cloud.contract.verifier.tests")
@Parameter
private String basePackageForTests;
@Parameter

View File

@@ -64,7 +64,7 @@ public class PluginIT {
.assertLogText("Generated 1 test classes.")
.assertLogText("Converting from Spring Cloud Contract Verifier contracts to WireMock stubs mappings")
.assertLogText("Creating new stub")
.assertLogText("Running org.springframework.cloud.contract.verifier.tests.ContractVerifierSpec")
.assertLogText("Running hello.ContractVerifierSpec")
.assertErrorFreeLog();
}
@@ -78,7 +78,7 @@ public class PluginIT {
.assertLogText("Generated 1 test classes.")
.assertLogText("Converting from Spring Cloud Contract Verifier contracts to WireMock stubs mappings")
.assertLogText("Creating new stub")
.assertLogText("Running org.springframework.cloud.contract.verifier.tests.ContractVerifierTest")
.assertLogText("Running hello.ContractVerifierTest")
.assertErrorFreeLog();
}
@@ -92,7 +92,7 @@ public class PluginIT {
.assertLogText("Generated 1 test classes.")
.assertLogText("Converting from Spring Cloud Contract Verifier contracts to WireMock stubs mappings")
.assertLogText("Creating new stub")
.assertLogText("Running org.springframework.cloud.contract.verifier.tests.ContractVerifierTest")
.assertLogText("Running hello.ContractVerifierTest")
.assertErrorFreeLog();
}

View File

@@ -16,12 +16,12 @@
*/
package org.springframework.cloud.contract.maven.verifier;
import io.takari.maven.testing.TestMavenRuntime;
import io.takari.maven.testing.TestResources;
import java.io.File;
import io.takari.maven.testing.TestMavenRuntime;
import io.takari.maven.testing.TestResources;
import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.util.StringUtils;
@@ -42,22 +42,26 @@ public class PluginUnitTest {
@Test
public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "convert");
this.maven.executeMojo(basedir, "convert", defaultPackageForTests());
assertFilesPresent(basedir, "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json".replace("/", File.separator));
assertFilesNotPresent(basedir, "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Messaging.json".replace("/", File.separator));
}
private Xpp3Dom defaultPackageForTests() {
return newParameter("basePackageForTests", "org.springframework.cloud.contract.verifier.tests");
}
@Test
public void shouldGenerateWireMockFromStubsDirectory() throws Exception {
File basedir = this.resources.getBasedir("withStubs");
this.maven.executeMojo(basedir, "convert", newParameter("contractsDirectory", "src/test/resources/stubs"));
this.maven.executeMojo(basedir, "convert", defaultPackageForTests(), newParameter("contractsDirectory", "src/test/resources/stubs"));
assertFilesPresent(basedir, "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json".replace("/", File.separator));
}
@Test
public void shouldCopyContracts() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "convert");
this.maven.executeMojo(basedir, "convert", defaultPackageForTests());
assertFilesPresent(basedir, "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/contracts/Sample.groovy".replace("/", File.separator));
assertFilesPresent(basedir, "target/stubs/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/contracts/Messaging.groovy".replace("/", File.separator));
}
@@ -65,14 +69,14 @@ public class PluginUnitTest {
@Test
public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "convert", newParameter("stubsDirectory", "target/foo"));
this.maven.executeMojo(basedir, "convert", defaultPackageForTests(), newParameter("stubsDirectory", "target/foo"));
assertFilesPresent(basedir, "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/Sample.json");
}
@Test
public void shouldGenerateContractSpecificationInDefaultLocation() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "SPOCK"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierSpec.groovy";
assertFilesPresent(basedir, path);
File test = new File(basedir, path);
@@ -82,7 +86,7 @@ public class PluginUnitTest {
@Test
public void shouldGenerateContractTestsInDefaultLocation() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "generateTests");
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests());
assertFilesPresent(basedir,
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
}
@@ -90,7 +94,7 @@ public class PluginUnitTest {
@Test
public void shouldGenerateContractTestsWithCustomImports() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "generateTests", newParameter("imports", ""));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("imports", ""));
assertFilesPresent(basedir,
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
}
@@ -98,7 +102,7 @@ public class PluginUnitTest {
@Test
public void shouldGenerateContractTestsWithoutArraySize() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "generateTests");
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests());
assertFilesPresent(basedir,
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
File test = new File(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
@@ -108,7 +112,7 @@ public class PluginUnitTest {
@Test
public void shouldGenerateContractTestsWithArraySize() throws Exception {
File basedir = this.resources.getBasedir("basic");
this.maven.executeMojo(basedir, "generateTests", newParameter("assertJsonSize", "true"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("assertJsonSize", "true"));
assertFilesPresent(basedir,
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
File test = new File(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
@@ -118,14 +122,14 @@ public class PluginUnitTest {
@Test
public void shouldGenerateStubs() throws Exception {
File basedir = this.resources.getBasedir("generatedStubs");
this.maven.executeMojo(basedir, "generateStubs");
this.maven.executeMojo(basedir, "generateStubs", defaultPackageForTests());
assertFilesPresent(basedir, "target/sample-project-0.1-stubs.jar");
}
@Test
public void shouldGenerateStubsWithMappingsOnly() throws Exception {
File basedir = this.resources.getBasedir("generatedStubs");
this.maven.executeMojo(basedir, "generateStubs", newParameter("attachContracts", "false"));
this.maven.executeMojo(basedir, "generateStubs", defaultPackageForTests(), newParameter("attachContracts", "false"));
assertFilesPresent(basedir, "target/sample-project-0.1-stubs.jar");
// FIXME: add assertion for jar content
}
@@ -133,21 +137,21 @@ public class PluginUnitTest {
@Test
public void shouldGenerateStubsWithCustomClassifier() throws Exception {
File basedir = this.resources.getBasedir("generatedStubs");
this.maven.executeMojo(basedir, "generateStubs", newParameter("classifier", "foo"));
this.maven.executeMojo(basedir, "generateStubs", defaultPackageForTests(), newParameter("classifier", "foo"));
assertFilesPresent(basedir, "target/sample-project-0.1-foo.jar");
}
@Test
public void shouldGenerateStubsByDownloadingContractsFromARepo() throws Exception {
File basedir = this.resources.getBasedir("basic-remote-contracts");
this.maven.executeMojo(basedir, "convert", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
this.maven.executeMojo(basedir, "convert", defaultPackageForTests(), newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
assertFilesPresent(basedir, "target/stubs/META-INF/com.example/server/0.1.BUILD-SNAPSHOT/mappings/com/example/server/client1/contracts/shouldMarkClientAsFraud.json");
}
@Test
public void shouldGenerateStubsByDownloadingContractsFromARepoWhenCustomPathIsProvided() throws Exception {
File basedir = this.resources.getBasedir("complex-remote-contracts");
this.maven.executeMojo(basedir, "convert", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
this.maven.executeMojo(basedir, "convert", defaultPackageForTests(), newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
assertFilesPresent(basedir, "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/mappings/com/example/server/client1/contracts/shouldMarkClientAsFraud.json");
assertFilesNotPresent(basedir, "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/mappings/com/foo/bar/baz/shouldBeIgnoredByPlugin.json");
assertFilesNotPresent(basedir, "target/stubs/META-INF/com.example.foo.bar.baz/someartifact/0.1.BUILD-SNAPSHOT/contracts/com/foo/bar/baz/shouldBeIgnoredByPlugin.groovy");
@@ -156,28 +160,28 @@ public class PluginUnitTest {
@Test
public void shouldGenerateOutputWhenCalledConvertFromRootProject() throws Exception {
File basedir = this.resources.getBasedir("different-module-configuration");
this.maven.executeMojo(basedir, "convert");
this.maven.executeMojo(basedir, "convert", defaultPackageForTests());
assertFilesPresent(basedir, "target/stubs/META-INF/com.blogspot.toomuchcoding.frauddetection/frauddetection-parent/0.1.0/mappings/shouldMarkClientAsFraud.json");
}
@Test
public void shouldGenerateOutputWhenCalledGenerateTestsFromRootProject() throws Exception {
File basedir = this.resources.getBasedir("different-module-configuration");
this.maven.executeMojo(basedir, "generateTests");
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests());
assertFilesPresent(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");
}
@Test
public void shouldGenerateTestsByDownloadingContractsFromARepo() throws Exception {
File basedir = this.resources.getBasedir("basic-remote-contracts");
this.maven.executeMojo(basedir, "generateTests", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
assertFilesPresent(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/example/server/client1/ContractsTest.java");
}
@Test
public void shouldGenerateTestsByDownloadingContractsFromARepoWhenCustomPathIsProvided() throws Exception {
File basedir = this.resources.getBasedir("complex-remote-contracts");
this.maven.executeMojo(basedir, "generateTests", newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("contractsRepositoryUrl", "file://" + PluginUnitTest.class.getClassLoader().getResource("m2repo/repository").getFile().replace("/", File.separator)));
assertFilesPresent(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/example/server/client1/ContractsTest.java");
assertFilesNotPresent(basedir, "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/foo/bar/BazTest.java");
assertFilesNotPresent(basedir, "target/stubs/contracts/com/foo/bar/baz/shouldBeIgnoredByPlugin.groovy");
@@ -187,7 +191,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsWithBaseClassResolvedFromConvention() throws Exception {
File basedir = this.resources.getBasedir("basic-generated-baseclass");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "JUNIT"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/hello/V1Test.java";
assertFilesPresent(basedir, path);
@@ -199,7 +203,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsWithBaseClassResolvedFromConventionForSpock() throws Exception {
File basedir = this.resources.getBasedir("basic-generated-baseclass");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "SPOCK"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/hello/V1Spec.groovy";
assertFilesPresent(basedir, path);
@@ -211,7 +215,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsWithBaseClassResolvedFromMapping() throws Exception {
File basedir = this.resources.getBasedir("basic-baseclass-from-mappings");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "JUNIT"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Test.java";
assertFilesPresent(basedir, path);
@@ -223,7 +227,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsWithBaseClassResolvedFromMappingNameForSpock() throws Exception {
File basedir = this.resources.getBasedir("basic-baseclass-from-mappings");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "SPOCK"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "SPOCK"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Spec.groovy";
assertFilesPresent(basedir, path);
@@ -235,7 +239,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsWithAFileContainingAListOfContracts() throws Exception {
File basedir = this.resources.getBasedir("multiple-contracts");
this.maven.executeMojo(basedir, "generateTests", newParameter("testFramework", "JUNIT"));
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests(), newParameter("testFramework", "JUNIT"));
String path = "target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/com/hello/V1Test.java";
assertFilesPresent(basedir, path);
@@ -249,7 +253,7 @@ public class PluginUnitTest {
public void shouldGenerateStubsWithAFileContainingAListOfContracts() throws Exception {
File basedir = this.resources.getBasedir("multiple-contracts");
this.maven.executeMojo(basedir, "convert", newParameter("stubsDirectory", "target/foo"));
this.maven.executeMojo(basedir, "convert", defaultPackageForTests(), newParameter("stubsDirectory", "target/foo"));
String firstFile = "target/foo/META-INF/org.springframework.cloud.verifier.sample/sample-project/0.1/mappings/com/hello/v1/should post a user.json";
File test = new File(basedir, firstFile);
@@ -265,7 +269,7 @@ public class PluginUnitTest {
public void shouldGenerateStubsForCommonRepoWithTargetFolder() throws Exception {
File basedir = this.resources.getBasedir("common-repo");
this.maven.executeMojo(basedir, "convert");
this.maven.executeMojo(basedir, "convert", defaultPackageForTests());
assertFilesNotPresent(basedir, "target/generated-test-sources/contracts/");
// there will be no stubs cause all files are copied to `target` folder
@@ -278,7 +282,7 @@ public class PluginUnitTest {
public void shouldGenerateContractTestsForPactAndMaintainIndents() throws Exception {
File basedir = this.resources.getBasedir("pact");
this.maven.executeMojo(basedir, "generateTests");
this.maven.executeMojo(basedir, "generateTests", defaultPackageForTests());
assertFilesPresent(basedir,
"target/generated-test-sources/contracts/org/springframework/cloud/contract/verifier/tests/ContractVerifierTest.java");

View File

@@ -16,9 +16,14 @@
package org.springframework.cloud.contract.verifier
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger
import com.google.common.collect.ListMultimap
import groovy.transform.PackageScope
import org.apache.commons.lang3.StringUtils
import org.springframework.cloud.contract.spec.ContractVerifierException
import org.springframework.cloud.contract.verifier.builder.JavaTestGenerator
import org.springframework.cloud.contract.verifier.builder.SingleTestGenerator
@@ -27,20 +32,21 @@ import org.springframework.cloud.contract.verifier.file.ContractFileScanner
import org.springframework.cloud.contract.verifier.file.ContractMetadata
import org.springframework.core.io.support.SpringFactoriesLoader
import java.nio.charset.StandardCharsets
import java.nio.file.Path
import java.util.concurrent.atomic.AtomicInteger
import static org.springframework.cloud.contract.verifier.util.NamesUtil.*
import static org.springframework.cloud.contract.verifier.util.NamesUtil.afterLast
import static org.springframework.cloud.contract.verifier.util.NamesUtil.beforeLast
import static org.springframework.cloud.contract.verifier.util.NamesUtil.convertIllegalPackageChars
import static org.springframework.cloud.contract.verifier.util.NamesUtil.directoryToPackage
import static org.springframework.cloud.contract.verifier.util.NamesUtil.toLastDot
/**
* @author Jakub Kubrynski, codearte.io
*/
class TestGenerator {
private final ContractVerifierConfigProperties configProperties
private final String DEFAULT_CLASS_PREFIX = "ContractVerifier"
private static final String DEFAULT_CLASS_PREFIX = "ContractVerifier"
private static final String DEFAULT_TEST_PACKAGE = "org.springframework.cloud.contract.verifier.tests"
private final ContractVerifierConfigProperties configProperties
private AtomicInteger counter = new AtomicInteger()
private SingleTestGenerator generator
private FileSaver saver
@@ -73,10 +79,21 @@ class TestGenerator {
}
int generate() {
generateTestClasses(configProperties.basePackageForTests)
generateTestClasses(basePackageName())
return counter.get()
}
private String basePackageName() {
if (configProperties.basePackageForTests) {
return configProperties.basePackageForTests
} else if (configProperties.baseClassForTests) {
return toLastDot(configProperties.baseClassForTests)
} else if (configProperties.packageWithBaseClasses) {
return configProperties.packageWithBaseClasses
}
return DEFAULT_TEST_PACKAGE
}
@PackageScope
void generateTestClasses(final String basePackageName) {
ListMultimap<Path, ContractMetadata> contracts = contractFileScanner.findContracts()

View File

@@ -572,6 +572,98 @@ class SingleTestGeneratorSpec extends Specification {
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from basePackageForTests when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
basePackageForTests: "a.b", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from baseClassForTests's package when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
baseClassForTests: "a.b.SomeClass", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a folder taken from packageWithBaseClasses when it is set for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
packageWithBaseClasses: "a.b", generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "a/b/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}
@Issue("#260")
def "should generate tests in a default folder when no property was passed for [#testFramework]"() {
given:
File contractLocation = new File(SingleTestGeneratorSpec.class.getResource("/classpath/readFromFile.groovy").toURI())
File temp = tmpFolder.newFolder()
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties(
targetFramework: testFramework, contractsDslDir: contractLocation.parentFile,
generatedTestSourcesDir: temp
)
TestGenerator testGenerator = new TestGenerator(properties)
when:
int count = testGenerator.generate()
then:
count == 1
and:
String test = new File(temp, "org/springframework/cloud/contract/verifier/tests/ContractVerifier" + (testFramework == JUNIT ? "Test.java" : "Spec.groovy")).text
test.contains("REQUEST")
test.contains("RESPONSE")
where:
testFramework << [JUNIT, SPOCK]
}