Fixed issues with ignored() contract-DSL for Junit, JUnit5 and Spock. Added tests. (spring-cloud#1346) (#1347)

This commit is contained in:
Roman Tsypuk
2020-03-16 09:25:11 +02:00
committed by GitHub
parent d0348dc885
commit 8532349ebb
4 changed files with 373 additions and 3 deletions

View File

@@ -46,7 +46,8 @@ class JUnit4IgnoreMethodAnnotation implements MethodAnnotations {
public boolean accept(SingleContractMetadata singleContractMetadata) {
return this.generatedClassMetaData.configProperties
.getTestFramework() == TestFramework.JUNIT
&& singleContractMetadata.getContractMetadata().isIgnored();
&& (singleContractMetadata.getContractMetadata().isIgnored()
|| singleContractMetadata.getContract().isIgnored());
}
}

View File

@@ -46,7 +46,8 @@ class JUnit5IgnoreMethodAnnotation implements MethodAnnotations {
public boolean accept(SingleContractMetadata singleContractMetadata) {
return this.generatedClassMetaData.configProperties
.getTestFramework() == TestFramework.JUNIT5
&& singleContractMetadata.getContractMetadata().isIgnored();
&& (singleContractMetadata.getContractMetadata().isIgnored()
|| singleContractMetadata.getContract().isIgnored());
}
}

View File

@@ -46,7 +46,8 @@ class SpockIgnoreMethodAnnotation implements MethodAnnotations {
public boolean accept(SingleContractMetadata singleContractMetadata) {
return this.generatedClassMetaData.configProperties
.getTestFramework() == TestFramework.SPOCK
&& singleContractMetadata.getContractMetadata().isIgnored();
&& (singleContractMetadata.getContractMetadata().isIgnored()
|| singleContractMetadata.getContract().isIgnored());
}
}

View File

@@ -458,6 +458,373 @@ class SingleTestGeneratorSpec extends Specification {
thrown UnsupportedOperationException
}
@Issue('#1346')
def 'should ignore 1 test if only 1-of-2 contracts is ignored in Contract dsl for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> false
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> false
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 1
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#1346')
def 'should ignore 2 tests if 2 contracts are ignored in Contract dsl for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> false
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> false
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 2
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#1346')
def 'should ignore 2 tests if 2 contracts are ignored in Contract dsl and in Configuration for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), true, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> true
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), true, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> true
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 2
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#1346')
def 'should ignore 1 tests if 1 of 2 contract is ignored in Contract dsl and in Configuration for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), true, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> true
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> false
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 1
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#1346')
def 'should ignore 2 tests if 1st contracts is ignored in Contract dsl and 2nd is ignored in Configuration for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
ignored()
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> false
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), true, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> true
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 2
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#1346')
def 'should ignore 0 tests if 0 contracts are ignored in Contract dsl and in Configuration for #testFramework'() {
given:
File firstFile = tmpFolder.newFile()
firstFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'GET'
url 'url'
}
response {
status OK()
}
}
''')
File secondFile = tmpFolder.newFile()
secondFile.write('''
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'POST'
url 'url'
}
response {
status OK()
}
}
''')
and:
ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties()
properties.testFramework = testFramework
and:
ContractMetadata firstContract = new ContractMetadata(firstFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), firstFile))
firstContract.ignored >> false
and:
ContractMetadata secondContract = new ContractMetadata(secondFile.
toPath(), false, 1, null, convertAsCollection(new File('/'), secondFile))
secondContract.ignored >> false
and:
JavaTestGenerator testGenerator = new JavaTestGenerator()
when:
String clazz = testGenerator.buildClass(properties, [secondContract, firstContract], 'com/foo', new SingleTestGenerator.GeneratedClassData('test', 'test', secondFile.toPath()))
then:
classStrings.each { clazz.contains(it) }
countOccurrencesOf(clazz, ignoreAnnotation) == 0
and:
asserter(clazz)
where:
testFramework | classStrings | ignoreAnnotation | asserter
JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER
JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER
TESTNG | mockMvcTestNGRestAssured3ClassStrings | '@Test(enabled = false)' | JAVA_ASSERTER
SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER
}
@Issue('#117')
def 'should generate test in explicit test mode using JUnit'() {
given: