From 8532349ebbf6d772febe831eaffdc9fba5d3c365 Mon Sep 17 00:00:00 2001 From: Roman Tsypuk Date: Mon, 16 Mar 2020 09:25:11 +0200 Subject: [PATCH] Fixed issues with ignored() contract-DSL for Junit, JUnit5 and Spock. Added tests. (spring-cloud#1346) (#1347) --- .../builder/JUnit4IgnoreMethodAnnotation.java | 3 +- .../builder/JUnit5IgnoreMethodAnnotation.java | 3 +- .../builder/SpockIgnoreMethodAnnotation.java | 3 +- .../builder/SingleTestGeneratorSpec.groovy | 367 ++++++++++++++++++ 4 files changed, 373 insertions(+), 3 deletions(-) diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit4IgnoreMethodAnnotation.java b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit4IgnoreMethodAnnotation.java index 3e5f07ff90..5c6075bfbc 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit4IgnoreMethodAnnotation.java +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit4IgnoreMethodAnnotation.java @@ -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()); } } diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit5IgnoreMethodAnnotation.java b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit5IgnoreMethodAnnotation.java index 0e2b16b0d0..fc08ce9b5f 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit5IgnoreMethodAnnotation.java +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/JUnit5IgnoreMethodAnnotation.java @@ -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()); } } diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/SpockIgnoreMethodAnnotation.java b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/SpockIgnoreMethodAnnotation.java index 852059afc8..bbd985dd5c 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/SpockIgnoreMethodAnnotation.java +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/builder/SpockIgnoreMethodAnnotation.java @@ -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()); } } diff --git a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy index 6ba35fba77..4338f8e445 100644 --- a/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy +++ b/spring-cloud-contract-verifier/src/test/groovy/org/springframework/cloud/contract/verifier/builder/SingleTestGeneratorSpec.groovy @@ -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: