diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy index bca38a0b1b..309173138f 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/SingleTestGenerator.groovy @@ -59,8 +59,6 @@ class SingleTestGenerator { if (configProperties.targetFramework == TestFramework.JUNIT) { clazz.addImport('org.junit.Test') - } else { - clazz.addImport('groovy.json.JsonSlurper') } if (configProperties.ruleClassForTests) { diff --git a/accurest-core/src/main/groovy/io/codearte/accurest/config/TestFramework.groovy b/accurest-core/src/main/groovy/io/codearte/accurest/config/TestFramework.groovy index c1014f19de..42d420ee7b 100644 --- a/accurest-core/src/main/groovy/io/codearte/accurest/config/TestFramework.groovy +++ b/accurest-core/src/main/groovy/io/codearte/accurest/config/TestFramework.groovy @@ -4,8 +4,8 @@ package io.codearte.accurest.config * @author Jakub Kubrynski */ enum TestFramework { - JUNIT("public ", "public void ", ";", ".java", "Test", "org.junit.Ignore", "org.junit.FixMethodOrder", "@FixMethodOrder(MethodSorters.NAME_ASCENDING)"), - SPOCK("", "def ", "", ".groovy", "Spec", "spock.lang.Ignore", "spock.lang.Stepwise", "@Stepwise") + JUNIT("public ", "public void ", ";", ".java", "Test", "org.junit.Ignore", ["org.junit.FixMethodOrder", "org.junit.runners.MethodSorters"], "@FixMethodOrder(MethodSorters.NAME_ASCENDING)"), + SPOCK("", "def ", "", ".groovy", "Spec", "spock.lang.Ignore", ["spock.lang.Stepwise"], "@Stepwise") private final String classModifier private final String methodModifier @@ -13,18 +13,18 @@ enum TestFramework { private final String classExtension; private final String classNameSuffix; private final String ignoreClass - private final String orderAnnotationImport + private final List orderAnnotationImports private final String orderAnnotation TestFramework(String classModifier, String methodModifier, String lineSuffix, String classExtension, String classNameSuffix, - String ignoreClass, String orderAnnotationImport, String orderAnnotation) { + String ignoreClass, List orderAnnotationImports, String orderAnnotation) { this.classModifier = classModifier this.lineSuffix = lineSuffix this.methodModifier = methodModifier this.classExtension = classExtension this.classNameSuffix = classNameSuffix this.ignoreClass = ignoreClass - this.orderAnnotationImport = orderAnnotationImport + this.orderAnnotationImports = orderAnnotationImports this.orderAnnotation = orderAnnotation } @@ -52,8 +52,8 @@ enum TestFramework { return ignoreClass } - String getOrderAnnotationImport() { - return orderAnnotationImport + List getOrderAnnotationImport() { + return orderAnnotationImports } String getOrderAnnotation() { diff --git a/accurest-core/src/test/groovy/io/codearte/accurest/SingleTestGeneratorSpec.groovy b/accurest-core/src/test/groovy/io/codearte/accurest/SingleTestGeneratorSpec.groovy new file mode 100644 index 0000000000..fbfa267ec4 --- /dev/null +++ b/accurest-core/src/test/groovy/io/codearte/accurest/SingleTestGeneratorSpec.groovy @@ -0,0 +1,67 @@ +package io.codearte.accurest + +import io.codearte.accurest.config.AccurestConfigProperties +import io.codearte.accurest.file.Contract +import org.junit.Rule +import org.junit.rules.TemporaryFolder +import spock.lang.Specification +import spock.lang.Unroll + +import static io.codearte.accurest.config.TestFramework.JUNIT +import static io.codearte.accurest.config.TestFramework.SPOCK + +class SingleTestGeneratorSpec extends Specification { + + @Rule + TemporaryFolder tmpFolder = new TemporaryFolder() + File file + + static List jUnitClassStrings = ['package test;', 'import com.jayway.jsonpath.DocumentContext;', 'import com.jayway.jsonpath.JsonPath;', + 'import org.junit.FixMethodOrder;', 'import org.junit.Ignore;', 'import org.junit.Test;', 'import org.junit.runners.MethodSorters;', + 'import static com.blogspot.toomuchcoding.jsonassert.JsonAssertion.assertThat;', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*;', + '@FixMethodOrder(MethodSorters.NAME_ASCENDING)', '@Test', '@Ignore'] + + static List spockClassStrings = ['package test', 'import com.jayway.jsonpath.DocumentContext', 'import com.jayway.jsonpath.JsonPath', + 'import spock.lang.Ignore', 'import spock.lang.Specification', 'import spock.lang.Stepwise', + 'import static com.blogspot.toomuchcoding.jsonassert.JsonAssertion.assertThat', 'import static com.jayway.restassured.module.mockmvc.RestAssuredMockMvc.*', + '@Stepwise', '@Ignore'] + + def setup() { + file = tmpFolder.newFile() + file.write(""" + io.codearte.accurest.dsl.GroovyDsl.make { + request { + method 'PUT' + url 'url' + } + response { + status 200 + } + } +""") + } + + @Unroll + def "should build test class for #testFramework"() { + given: + AccurestConfigProperties properties = new AccurestConfigProperties(); + properties.targetFramework = testFramework + Contract contract = new Contract(file.toPath(), true, 1, 2) + contract.ignored >> true + contract.order >> 2 + SingleTestGenerator testGenerator = new SingleTestGenerator(properties) + + when: + String clazz = testGenerator.buildClass([contract], "test", "test") + + then: + classStrings.forEach({ clazz.contains(it) }) + + where: + testFramework | classStrings + JUNIT | jUnitClassStrings + SPOCK | spockClassStrings + } + + +}