Added option of multiple test base classes
without this change the user was forced to use a single base class for all of the generated tests. It could become problematic after some time. With this change we provide a range of options of providing different base classes for different contracts. fixes #16
This commit is contained in:
@@ -112,11 +112,38 @@ class ContractVerifierExtension {
|
||||
*/
|
||||
boolean contractsWorkOffline
|
||||
|
||||
/**
|
||||
* A package that contains all the base clases for generated tests. If your contract resides in a location
|
||||
* {@code src/test/resources/contracts/com/example/v1/} and you provide the {@code packageWithBaseClasses}
|
||||
* value to {@code com.example.contracts.base} then we will search for a test source file that will
|
||||
* have the package {@code com.example.contracts.base} and name {@code ExampleV1Base}. As you can see
|
||||
* it will take the two last folders to and attach {@code Base} to its name.
|
||||
*/
|
||||
String packageWithBaseClasses
|
||||
|
||||
/**
|
||||
* A way to override any base class mappings. The keys are regular expressions on the package name
|
||||
* and the values FQN to a base class for that given expression.
|
||||
* </p>
|
||||
* Example of a mapping
|
||||
* </p>
|
||||
* {@code .*.com.example.v1..*} -> {@code com.example.SomeBaseClass}
|
||||
* </p>
|
||||
* When a contract's package matches the provided regular expression then extending class will be the one
|
||||
* provided in the map - in this case {@code com.example.SomeBaseClass}
|
||||
*/
|
||||
Map<String, String> baseClassMappings = [:]
|
||||
|
||||
void contractDependency(@DelegatesTo(Dependency) Closure closure) {
|
||||
closure.delegate = contractDependency
|
||||
closure.call()
|
||||
}
|
||||
|
||||
void baseClassMappings(@DelegatesTo(BaseClassMapping) Closure closure) {
|
||||
closure.delegate = new BaseClassMapping(baseClassMappings)
|
||||
closure.call()
|
||||
}
|
||||
|
||||
static class Dependency {
|
||||
String groupId
|
||||
String artifactId
|
||||
@@ -124,4 +151,20 @@ class ContractVerifierExtension {
|
||||
String version
|
||||
String stringNotation
|
||||
}
|
||||
|
||||
static class BaseClassMapping {
|
||||
private final Map<String, String> delegate
|
||||
|
||||
BaseClassMapping(Map<String, String> delegate) {
|
||||
this.delegate = delegate
|
||||
}
|
||||
|
||||
void baseClassMapping(String packageRegex, String fqnBaseClass) {
|
||||
this.delegate[packageRegex] = fqnBaseClass
|
||||
}
|
||||
|
||||
void baseClassMapping(Map mapping) {
|
||||
this.delegate.putAll(mapping)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@ class ExtensionToProperties {
|
||||
generatedTestSourcesDir: extension.generatedTestSourcesDir,
|
||||
stubsOutputDir: extension.stubsOutputDir,
|
||||
stubsSuffix: extension.stubsSuffix,
|
||||
assertJsonSize: extension.assertJsonSize
|
||||
assertJsonSize: extension.assertJsonSize,
|
||||
packageWithBaseClasses: extension.packageWithBaseClasses,
|
||||
baseClassMappings: extension.baseClassMappings
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ abstract class ContractVerifierIntegrationSpec extends Specification {
|
||||
|
||||
public static final String SPOCK = "targetFramework = 'Spock'"
|
||||
public static final String JUNIT = "targetFramework = 'JUnit'"
|
||||
public static final String MVC_SPEC = "baseClassForTests = 'org.springframework.cloud.MvcSpec'"
|
||||
public static final String MVC_TEST = "baseClassForTests = 'org.springframework.cloud.MvcTest'"
|
||||
public static final String MVC_SPEC = "'org.springframework.cloud.MvcSpec'"
|
||||
public static final String MVC_TEST = "'org.springframework.cloud.MvcTest'"
|
||||
protected static final boolean WORK_OFFLINE = Boolean.parseBoolean(System.getProperty('WORK_OFFLINE', 'false'))
|
||||
|
||||
File testProjectDir
|
||||
|
||||
@@ -138,4 +138,25 @@ class ContractVerifierSpec extends Specification {
|
||||
it.group == "org.assertj" && it.name == "assertj-core"
|
||||
} != null
|
||||
}
|
||||
|
||||
def "should compile"() {
|
||||
given:
|
||||
ContractVerifierExtension extension = new ContractVerifierExtension()
|
||||
extension.with {
|
||||
|
||||
// tag::package_with_base_classes[]
|
||||
packageWithBaseClasses = 'com.example.base'
|
||||
// end::package_with_base_classes[]
|
||||
|
||||
// tag::base_class_mappings[]
|
||||
baseClassForTests = "com.example.FooBase"
|
||||
baseClassMappings {
|
||||
baseClassMapping('.*/com/.*', 'com.example.ComBase')
|
||||
baseClassMapping('.*/bar/.*':'com.example.BarBase')
|
||||
}
|
||||
// end::base_class_mappings[]
|
||||
}
|
||||
expect:
|
||||
extension
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,12 @@ configure([project(':fraudDetectionService'), project(':loanApplicationService')
|
||||
targetFramework = 'Spock'
|
||||
// end::target_framework[]
|
||||
testMode = 'MockMvc'
|
||||
baseClassForTests = 'org.springframework.cloud.MvcSpec'
|
||||
//baseClassForTests = 'org.springframework.cloud.MvcSpec'
|
||||
// tag::base_class_mapping[]
|
||||
baseClassMappings {
|
||||
baseClassMapping('.*', 'org.springframework.cloud.MvcSpec')
|
||||
}
|
||||
// end::base_class_mapping[]
|
||||
contractsDslDir = file("${project.projectDir.absolutePath}/mappings/")
|
||||
generatedTestSourcesDir = file("${project.buildDir}/generated-test-sources/")
|
||||
stubsOutputDir = stubsOutputDirRoot
|
||||
|
||||
Reference in New Issue
Block a user