From 60809445ffe6f58be005c1c2c9e9fa22ca04a967 Mon Sep 17 00:00:00 2001 From: Olga Maciaszek-Sharma Date: Tue, 20 Aug 2019 16:09:16 +0200 Subject: [PATCH] Add test scenarios to JUnit5. Fixes gh-887. (#1177) --- spring-cloud-contract-verifier/pom.xml | 3 + .../config/framework/JUnit5Definition.groovy | 5 +- .../builder/SingleTestGeneratorSpec.groovy | 88 +++++++++---------- 3 files changed, 46 insertions(+), 50 deletions(-) diff --git a/spring-cloud-contract-verifier/pom.xml b/spring-cloud-contract-verifier/pom.xml index 84ca5afa79..4ef3b3d4de 100644 --- a/spring-cloud-contract-verifier/pom.xml +++ b/spring-cloud-contract-verifier/pom.xml @@ -166,6 +166,9 @@ org.junit.jupiter junit-jupiter-api + + ${junit-jupiter.version} + test org.springframework.boot diff --git a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/framework/JUnit5Definition.groovy b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/framework/JUnit5Definition.groovy index c8917eccbd..c4511fcf6c 100644 --- a/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/framework/JUnit5Definition.groovy +++ b/spring-cloud-contract-verifier/src/main/groovy/org/springframework/cloud/contract/verifier/config/framework/JUnit5Definition.groovy @@ -60,12 +60,13 @@ class JUnit5Definition implements TestFrameworkDefinition { @Override List getOrderAnnotationImports() { - throw new UnsupportedOperationException('Not implemented yet in JUnit5 - https://github.com/junit-team/junit5/issues/48') + return ['org.junit.jupiter.api.TestMethodOrder', + 'org.junit.jupiter.api.MethodOrderer'] } @Override String getOrderAnnotation() { - throw new UnsupportedOperationException('Not implemented yet in JUnit5 - https://github.com/junit-team/junit5/issues/48') + return '@TestMethodOrder(MethodOrderer.Alphanumeric.class)' } @Override 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 b84303e48f..62e77f54e9 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 @@ -73,28 +73,31 @@ class SingleTestGeneratorSpec extends Specification { 'import org.junit.jupiter.api.Disabled;', 'import org.junit.jupiter.api.Test;', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*;', '@Test', '@Disabled', 'import com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification;', - 'import com.jayway.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] + 'import com.jayway.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat', + 'import org.junit.jupiter.api.TestMethodOrder', 'import org.junit.jupiter.api.MethodOrderer', '@TestMethodOrder(MethodOrderer.Alphanumeric.class)'] private static final List mockMvcJUnit5RestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', 'import org.junit.jupiter.api.Disabled;', 'import org.junit.jupiter.api.Test;', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.module.mockmvc.RestAssuredMockMvc.*;', '@Test', '@Disabled', 'import io.restassured.module.mockmvc.specification.MockMvcRequestSpecification;', - 'import io.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] + 'import io.restassured.response.ResponseOptions;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat', + 'import org.junit.jupiter.api.TestMethodOrder', 'import org.junit.jupiter.api.MethodOrderer', '@TestMethodOrder(MethodOrderer.Alphanumeric.class)'] private static final List explicitJUnit5RestAssured2ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', 'import org.junit.jupiter.api.Disabled;', 'import org.junit.jupiter.api.Test;', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static com.jayway.restassured.RestAssured.*;', '@Test', '@Disabled', 'import com.jayway.restassured.specification.RequestSpecification;', - 'import com.jayway.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] + 'import com.jayway.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat', + 'import org.junit.jupiter.api.TestMethodOrder', 'org.junit.jupiter.api.MethodOrderer', '@TestMethodOrder(MethodOrderer.Alphanumeric.class)'] private static final List explicitJUnit5RestAssured3ClassStrings = ['import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', 'import org.junit.jupiter.api.Disabled;', 'import org.junit.jupiter.api.Test;', 'import static com.toomuchcoding.jsonassert.JsonAssertion.assertThatJson;', 'import static io.restassured.RestAssured.*;', '@Test', '@Disabled', 'import io.restassured.specification.RequestSpecification;', - 'import io.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat'] - + 'import io.restassured.response.Response;', 'import static org.springframework.cloud.contract.verifier.assertion.SpringCloudContractAssertions.assertThat', + 'org.junit.jupiter.api.TestMethodOrder', 'org.junit.jupiter.api.MethodOrderer', '@TestMethodOrder(MethodOrderer.Alphanumeric.class)'] private static final List spockClassRestAssured2Strings = ['import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', @@ -166,12 +169,13 @@ class SingleTestGeneratorSpec extends Specification { ''') } - def 'should build test class for #testFramework'() { + def 'should build test class for #testFramework - #mode'() { given: ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() properties.testFramework = testFramework properties.testMode = mode - ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, order, convertAsCollection(new File('/'), file)) + ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, + convertAsCollection(new File('/'), file)) contract.ignored >> true JavaTestGenerator testGenerator = new JavaTestGenerator() @@ -183,13 +187,13 @@ class SingleTestGeneratorSpec extends Specification { and: asserter(clazz) where: - testFramework | order | mode | classStrings | asserter - JUNIT | 2 | MOCKMVC | mockMvcJUnitRestAssured3ClassStrings | JAVA_ASSERTER - JUNIT | 2 | EXPLICIT | explicitJUnitRestAssured3ClassStrings | JAVA_ASSERTER - JUNIT5 | null | MOCKMVC | mockMvcJUnit5RestAssured3ClassStrings | JAVA_ASSERTER - JUNIT5 | null | EXPLICIT | explicitJUnit5RestAssured3ClassStrings | JAVA_ASSERTER - SPOCK | 2 | MOCKMVC | spockClassRestAssured3Strings | GROOVY_ASSERTER - SPOCK | 2 | EXPLICIT | explicitSpockRestAssured3ClassStrings | GROOVY_ASSERTER + testFramework | mode | classStrings | asserter + JUNIT | MOCKMVC | mockMvcJUnitRestAssured3ClassStrings | JAVA_ASSERTER + JUNIT | EXPLICIT | explicitJUnitRestAssured3ClassStrings | JAVA_ASSERTER + JUNIT5 | MOCKMVC | mockMvcJUnit5RestAssured3ClassStrings | JAVA_ASSERTER + JUNIT5 | EXPLICIT | explicitJUnit5RestAssured3ClassStrings | JAVA_ASSERTER + SPOCK | MOCKMVC | spockClassRestAssured3Strings | GROOVY_ASSERTER + SPOCK | EXPLICIT | explicitSpockRestAssured3ClassStrings | GROOVY_ASSERTER } def 'should build test class for #testFramework when the path contains bizarre signs'() { @@ -227,7 +231,7 @@ class SingleTestGeneratorSpec extends Specification { ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() properties.testFramework = testFramework properties.testMode = mode - ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, order, + ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, convertAsCollection(new File('/'), file)) contract.ignored >> true JavaTestGenerator testGenerator = new JavaTestGenerator(checker: new ClassPresenceChecker() { @@ -246,13 +250,13 @@ class SingleTestGeneratorSpec extends Specification { !clazz.contains('io.restassured') where: - testFramework | order | mode | classStrings - JUNIT | 2 | MOCKMVC | mockMvcJUnitRestAssured2ClassStrings - JUNIT | 2 | EXPLICIT | explicitJUnitRestAssured2ClassStrings - JUNIT5 | null | MOCKMVC | mockMvcJUnit5RestAssured2ClassStrings - JUNIT5 | null | EXPLICIT | explicitJUnit5RestAssured2ClassStrings - SPOCK | 2 | MOCKMVC | spockClassRestAssured2Strings - SPOCK | 2 | EXPLICIT | explicitSpockRestAssured2ClassStrings + testFramework | mode | classStrings + JUNIT | MOCKMVC | mockMvcJUnitRestAssured2ClassStrings + JUNIT | EXPLICIT | explicitJUnitRestAssured2ClassStrings + JUNIT5 | MOCKMVC | mockMvcJUnit5RestAssured2ClassStrings + JUNIT5 | EXPLICIT | explicitJUnit5RestAssured2ClassStrings + SPOCK | MOCKMVC | spockClassRestAssured2Strings + SPOCK | EXPLICIT | explicitSpockRestAssured2ClassStrings } def 'should build test class for #testFramework and mode #mode with two files'() { @@ -375,10 +379,12 @@ class SingleTestGeneratorSpec extends Specification { and: ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() properties.testFramework = testFramework - ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, order, convertAsCollection(new File('/'), file)) + ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 2, + convertAsCollection(new File('/'), file)) contract.ignored >> true and: - ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, order, convertAsCollection(new File('/'), secondFile)) + ContractMetadata contract2 = new ContractMetadata(secondFile. + toPath(), true, 1, 2, convertAsCollection(new File('/'), secondFile)) contract2.ignored >> true and: JavaTestGenerator testGenerator = new JavaTestGenerator() @@ -394,10 +400,10 @@ class SingleTestGeneratorSpec extends Specification { asserter(clazz) where: - testFramework | order | classStrings | asserter - JUNIT | 2 | mockMvcJUnitRestAssured3ClassStrings | JAVA_ASSERTER - JUNIT5 | null | mockMvcJUnit5RestAssured3ClassStrings | JAVA_ASSERTER - SPOCK | 2 | spockClassRestAssured3Strings | GROOVY_ASSERTER + testFramework | classStrings | asserter + JUNIT | mockMvcJUnitRestAssured3ClassStrings | JAVA_ASSERTER + JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | JAVA_ASSERTER + SPOCK | spockClassRestAssured3Strings | GROOVY_ASSERTER } @Issue('#30') @@ -420,7 +426,8 @@ class SingleTestGeneratorSpec extends Specification { ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() properties.testFramework = testFramework and: - ContractMetadata contract2 = new ContractMetadata(secondFile.toPath(), true, 1, order, convertAsCollection(new File('/'), file)) + ContractMetadata contract2 = new ContractMetadata(secondFile. + toPath(), true, 1, 2, convertAsCollection(new File('/'), file)) contract2.ignored >> false and: JavaTestGenerator testGenerator = new JavaTestGenerator() @@ -436,10 +443,10 @@ class SingleTestGeneratorSpec extends Specification { asserter(clazz) where: - testFramework | order | classStrings | ignoreAnnotation | asserter - JUNIT | 2 | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER - JUNIT5 | null | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER - SPOCK | 2 | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER + testFramework | classStrings | ignoreAnnotation | asserter + JUNIT | mockMvcJUnitRestAssured3ClassStrings | '@Ignore' | JAVA_ASSERTER + JUNIT5 | mockMvcJUnit5RestAssured3ClassStrings | '@Disabled' | JAVA_ASSERTER + SPOCK | spockClassRestAssured3Strings | '@Ignore' | GROOVY_ASSERTER } @Issue('#117') @@ -716,21 +723,6 @@ class SingleTestGeneratorSpec extends Specification { testFramework << [JUNIT, JUNIT5, SPOCK] } - def 'should throw exception in JUnit5 when contract belongs to scenario'() { - given: - ContractVerifierConfigProperties properties = new ContractVerifierConfigProperties() - properties.testFramework = JUNIT5 - properties.testMode = mode - ContractMetadata contract = new ContractMetadata(file.toPath(), true, 1, 1, convertAsCollection(new File('/'), file)) - JavaTestGenerator testGenerator = new JavaTestGenerator() - when: - testGenerator.buildClass(properties, [contract], 'test', 'test', 'com/foo') - then: - thrown(UnsupportedOperationException) - where: - mode << [MOCKMVC, EXPLICIT, JAXRSCLIENT] - } - private static String getTestName(TestFramework testFramework) { testFramework == SPOCK ? 'Spec.groovy' : 'Test.java' }