Fixed class imports for JUnit. Added test for class imports and annotations.
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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<String> 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<String> 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<String> getOrderAnnotationImport() {
|
||||
return orderAnnotationImports
|
||||
}
|
||||
|
||||
String getOrderAnnotation() {
|
||||
|
||||
@@ -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<String> 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<String> 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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user