Fixed selecting the right method builder for test framework.

Added functional tests for JUnit.
This commit is contained in:
Olga Maciaszek-Sharma
2016-02-23 14:09:19 +01:00
parent b507a5169a
commit 723aa2d37b
8 changed files with 160 additions and 3 deletions

View File

@@ -62,7 +62,12 @@ class SingleTestGenerator {
}
if (configProperties.targetFramework == TestFramework.JUNIT) {
if (configProperties.testMode == TestMode.MOCKMVC) {
clazz.addImport('com.jayway.restassured.module.mockmvc.specification.MockMvcRequestSpecification')
clazz.addImport('com.jayway.restassured.response.ResponseOptions')
}
clazz.addImport('org.junit.Test')
clazz.addStaticImport('org.assertj.core.api.Assertions.assertThat')
}
if (configProperties.ruleClassForTests) {

View File

@@ -48,8 +48,14 @@ class MethodBuilder {
blockBuilder.addLine('}')
}
private SpockMethodBodyBuilder getMethodBodyBuilder() {
private MethodBodyBuilder getMethodBodyBuilder() {
if (configProperties.testMode == TestMode.MOCKMVC && configProperties.targetFramework == TestFramework.JUNIT){
return new MockMvcJUnitMethodBodyBuilder(stubContent)
}
if (configProperties.testMode == TestMode.JAXRSCLIENT) {
if (configProperties.targetFramework == TestFramework.JUNIT){
return new JaxRsClientJUnitMethodBodyBuilder(stubContent)
}
return new JaxRsClientSpockMethodBodyBuilder(stubContent)
}
return new MockMvcSpockMethodBodyBuilder(stubContent)

View File

@@ -30,6 +30,7 @@ class AccurestGradlePlugin implements Plugin<Project> {
deprecatedCreateAndConfigureGenerateWiremockClientStubsFromDslTask()
project.dependencies.add("testCompile", "com.github.tomakehurst:wiremock:2.0.5-beta")
project.dependencies.add("testCompile", "com.blogspot.toomuchcoding:jsonassert:${extension.getJsonAssertVersion()}")
project.dependencies.add("testCompile", "org.assertj:assertj-core:3.3.0")
project.afterEvaluate {
def hasIdea = project.plugins.findPlugin(IDEA_PLUGIN_CLASS)

View File

@@ -3,19 +3,47 @@ package io.codearte.accurest.plugin
import nebula.test.IntegrationSpec
import spock.lang.Stepwise
import java.nio.file.Files
import java.nio.file.Path
import static java.nio.charset.StandardCharsets.UTF_8
import static java.nio.charset.StandardCharsets.UTF_8
@Stepwise
class SampleJerseyProjectSpec extends IntegrationSpec {
public static final String SPOCK = "targetFramework = 'Spock'"
public static final String JUNIT = "targetFramework = 'JUnit'"
public static final String MVC_SPEC = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcSpec'"
public static final String MVC_TEST = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcTest'"
void setup() {
copyResources("functionalTest/sampleJerseyProject", "")
runTasksSuccessfully('clean') //delete accidental output when previously importing SimpleBoot into Idea to tweak it
}
def "should pass basic flow"() {
def "should pass basic flow for Spock"() {
given:
assert fileExists('build.gradle')
expect:
runTasksSuccessfully('check')
}
def "should pass basic flow for JUnit"() {
given:
switchToJunitTestFramework()
assert fileExists('build.gradle')
expect:
runTasksSuccessfully('check')
}
private void switchToJunitTestFramework() {
Path path = buildFile.toPath()
String content = new StringBuilder(new String(Files.readAllBytes(path), UTF_8)).replaceAll(SPOCK, JUNIT)
.replaceAll(MVC_SPEC, MVC_TEST)
Files.write(path, content.getBytes(UTF_8))
}
}

View File

@@ -2,20 +2,48 @@ package io.codearte.accurest.plugin
import nebula.test.IntegrationSpec
import spock.lang.Stepwise
import spock.lang.Unroll
import java.nio.file.Files
import java.nio.file.Path
import static java.nio.charset.StandardCharsets.UTF_8
import static java.nio.charset.StandardCharsets.UTF_8
@Stepwise
class SampleProjectSpec extends IntegrationSpec {
public static final String SPOCK = "targetFramework = 'Spock'"
public static final String JUNIT = "targetFramework = 'JUnit'"
public static final String MVC_SPEC = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcSpec'"
public static final String MVC_TEST = "baseClassForTests = 'com.blogspot.toomuchcoding.MvcTest'"
void setup() {
copyResources("functionalTest/sampleProject", "")
runTasksSuccessfully('clean') //delete accidental output when previously importing SimpleBoot into Idea to tweak it
}
def "should pass basic flow"() {
def "should pass basic flow for Spock"() {
given:
assert fileExists('build.gradle')
expect:
runTasksSuccessfully('check')
}
def "should pass basic flow for JUnit"() {
given:
switchToJunitTestFramework()
assert fileExists('build.gradle')
expect:
runTasksSuccessfully('check')
}
private void switchToJunitTestFramework() {
Path path = buildFile.toPath()
String content = new StringBuilder(new String(Files.readAllBytes(path), UTF_8)).replaceAll(SPOCK, JUNIT)
.replaceAll(MVC_SPEC, MVC_TEST)
Files.write(path, content.getBytes(UTF_8))
}
}

View File

@@ -0,0 +1,70 @@
package com.blogspot.toomuchcoding;
import com.blogspot.toomuchcoding.frauddetection.Application;
import com.blogspot.toomuchcoding.frauddetection.FraudRestApplication;
import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.UriBuilder;
import java.net.URI;
import static org.springframework.util.SocketUtils.findAvailableTcpPort;
public abstract class MvcTest {
public static WebTarget webTarget;
private static Server server;
private static Client client;
@BeforeClass
public static void setupTest() {
URI baseUri = UriBuilder.fromUri("http://localhost").port(findAvailableTcpPort(8000)).build();
ResourceConfig resourceConfig = ResourceConfig.forApplication(new FraudRestApplication());
resourceConfig.property("contextConfig", new AnnotationConfigApplicationContext(Application.class));
server = JettyHttpContainerFactory.createServer(baseUri, resourceConfig, true);
ClientConfig clientConfig = new ClientConfig();
clientConfig.connectorProvider(new ApacheConnectorProvider());
client = ClientBuilder.newClient(clientConfig);
webTarget = client.target(baseUri);
try {
server.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterClass
public static void cleanupTest() {
if(client != null) {
client.close();
}
if (server != null) {
try {
server.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void assertThatRejectionReasonIsNull(Object rejectionReason) {
assert rejectionReason == null;
}
}

View File

@@ -0,0 +1,18 @@
package com.blogspot.toomuchcoding;
import com.blogspot.toomuchcoding.frauddetection.FraudDetectionController;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import spock.lang.Specification;
import org.junit.Before;
public class MvcTest {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new com.blogspot.toomuchcoding.frauddetection.FraudDetectionController());
}
public void assertThatRejectionReasonIsNull(Object rejectionReason) {
assert rejectionReason== null;
}
}

View File

@@ -73,6 +73,7 @@ project(':accurest-core') {
compile 'asm:asm:3.3.1'
compile "com.github.tomakehurst:wiremock:$wiremockVersion"
compile "com.blogspot.toomuchcoding:jsonassert:$jsonassertVersion"
compile 'org.assertj:assertj-core:[3.3.0,)'
testCompile 'cglib:cglib-nodep:2.2'
testCompile 'org.objenesis:objenesis:2.1'
testCompile project(':accurest-testing-utils')