separate generateTests for junit verification code

This commit is contained in:
Mariusz Smykula
2016-04-02 13:50:04 +02:00
parent b662c5218a
commit 033eb6fbf6
24 changed files with 368 additions and 69 deletions

View File

@@ -31,7 +31,12 @@
<artifactId>logback-core</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>

View File

@@ -0,0 +1,70 @@
package io.codearte.accurest.maven;
import static java.lang.String.format;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Parameter;
import org.assertj.core.util.Strings;
import io.codearte.accurest.AccurestException;
import io.codearte.accurest.TestGenerator;
import io.codearte.accurest.config.AccurestConfigProperties;
import io.codearte.accurest.config.TestFramework;
import io.codearte.accurest.config.TestMode;
public abstract class AbstractGenerateVerificationCodeMojo extends AbstractMojo {
@Parameter(defaultValue = "${basedir}", readonly = true, required = true)
protected File baseDir;
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
protected File projectBuildDirectory;
@Parameter(defaultValue = "/src/test/accurest")
protected String contractsDir;
@Parameter(defaultValue = "/generated-test-sources/accurest")
protected String generatedTestSourcesDir;
@Parameter(defaultValue = "io.codearte.accurest.tests")
protected String basePackageForTests;
@Parameter
protected String baseClassForTests;
@Parameter(defaultValue = "MOCKMVC")
protected TestMode testMode;
protected void generateVerificationCode(TestFramework testFramework) throws MojoExecutionException {
AccurestConfigProperties config = new AccurestConfigProperties();
config.setContractsDslDir(new File(baseDir, contractsDir));
config.setBasePackageForTests(basePackageForTests);
config.setGeneratedTestSourcesDir(new File(projectBuildDirectory, generatedTestSourcesDir));
if (Strings.isNullOrEmpty(baseClassForTests)) {
if (!Strings.isNullOrEmpty(basePackageForTests)) {
baseClassForTests = basePackageForTests + '.' + "BaseAccurest";
} else {
baseClassForTests = "io.codearte.accurest.tests.BaseAccurest";
}
}
config.setBaseClassForTests(baseClassForTests);
config.setTargetFramework(testFramework);
config.setTestMode(testMode);
getLog().info(format("Using %s as test source directory", config.getGeneratedTestSourcesDir()));
getLog().info(format("Using %s as base class for verification classes", config.getBaseClassForTests()));
try {
TestGenerator generator = new TestGenerator(config);
int generatedClasses = generator.generate();
getLog().info(format("Generated %s test classes.", generatedClasses));
} catch (AccurestException e) {
throw new MojoExecutionException(format("Accurest Plugin exception: %s", e.getMessage()), e);
}
}
}

View File

@@ -1,74 +1,18 @@
package io.codearte.accurest.maven;
import static java.lang.String.format;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import io.codearte.accurest.AccurestException;
import io.codearte.accurest.TestGenerator;
import io.codearte.accurest.config.AccurestConfigProperties;
import io.codearte.accurest.config.TestFramework;
import io.codearte.accurest.config.TestMode;
@Mojo(name = "generateSpecs", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES)
public class GenerateSpecsMojo extends AbstractMojo {
@Parameter(defaultValue = "${basedir}", readonly = true, required = true)
private File baseDir;
@Parameter(defaultValue = "${project.build.directory}", readonly = true, required = true)
private File projectBuildDirectory;
@Parameter(defaultValue = "/src/test/accurest")
private String contractsDir;
@Parameter(defaultValue = "/generated-test-sources/accurest")
private String generatedTestSourcesDir;
@Parameter(defaultValue = "io.codearte.accurest.tests")
private String basePackageForTests;
@Parameter
private String baseClassForTests;
@Parameter(defaultValue = "SPOCK")
private TestFramework targetFramework;
@Parameter(defaultValue = "MOCKMVC")
private TestMode testMode;
public GenerateSpecsMojo() {
}
public class GenerateSpecsMojo extends AbstractGenerateVerificationCodeMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
AccurestConfigProperties config = new AccurestConfigProperties();
config.setContractsDslDir(new File(baseDir, contractsDir));
config.setBasePackageForTests(basePackageForTests);
config.setGeneratedTestSourcesDir(new File(projectBuildDirectory, generatedTestSourcesDir));
config.setBaseClassForTests(baseClassForTests);
config.setTargetFramework(targetFramework);
config.setTestMode(testMode);
getLog().info("Accurest Plugin: Invoking test sources generation");
getLog().info(format("Using %s as test source directory", config.getGeneratedTestSourcesDir()));
getLog().info(format("Using %s as base class for tests", config.getBaseClassForTests()));
try {
TestGenerator generator = new TestGenerator(config);
int generatedClasses = generator.generate();
getLog().info(format("Generated %s test classes.", generatedClasses));
} catch (AccurestException e) {
throw new MojoExecutionException(format("Accurest Plugin exception: %s", e.getMessage()), e);
}
getLog().info("Generating Spock Specifications source code for Accurest contract verification");
generateVerificationCode(TestFramework.SPOCK);
}
}

View File

@@ -0,0 +1,18 @@
package io.codearte.accurest.maven;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import io.codearte.accurest.config.TestFramework;
@Mojo(name = "generateTests", defaultPhase = LifecyclePhase.GENERATE_TEST_SOURCES)
public class GenerateTestsMojo extends AbstractGenerateVerificationCodeMojo {
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Generating JUnit Tests source code for Accurest contract verification");
generateVerificationCode(TestFramework.JUNIT);
}
}

View File

@@ -28,12 +28,24 @@ public class PluginIntegrationTest {
}
@Test
public void shouldBuildSimpleBootWIthAccurtesProject() throws Exception {
File basedir = resources.getBasedir("bootSimple");
public void should_build_project_Spring_Boot_Groovy_with_Accurest() throws Exception {
File basedir = resources.getBasedir("spring-boot-groovy");
maven.forProject(basedir)
.execute("package")
.assertErrorFreeLog()
.assertLogText("Accurest Plugin: Invoking test sources generation")
.assertLogText("Generating Spock Specifications source code for Accurest contract verification")
.assertLogText("Generated 1 test classes.")
.assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion")
.assertLogText("Creating new json")
.assertErrorFreeLog();
}
@Test
public void should_build_project_Spring_Boot_Java_with_Accurest() throws Exception {
File basedir = resources.getBasedir("spring-boot-java");
maven.forProject(basedir)
.execute("package")
.assertErrorFreeLog()
.assertLogText("Generating JUnit Tests source code for Accurest contract verification")
.assertLogText("Generated 1 test classes.")
.assertLogText("Accurest Plugin: Invoking GroovyDSL to WireMock client stubs conversion")
.assertLogText("Creating new json")
@@ -41,7 +53,7 @@ public class PluginIntegrationTest {
}
@Test
public void shouldConvertAccurestToWireMockStubs() throws Exception {
public void should_convert_Accurest_Contracts_to_WireMock_Stubs_mappings() throws Exception {
File basedir = resources.getBasedir("pomless");
properties.getPluginVersion();
maven.forProject(basedir)

View File

@@ -1,5 +1,6 @@
package io.codearte.accurest.maven;
import static io.takari.maven.testing.TestMavenRuntime.newParameter;
import static io.takari.maven.testing.TestResources.assertFilesPresent;
import java.io.File;
@@ -19,23 +20,23 @@ public class PluginUnitTest {
public final TestMavenRuntime maven = new TestMavenRuntime();
@Test
public void shouldGenerateWiremockStubsInDefaultcLocation() throws Exception {
public void shouldGenerateWireMockStubsInDefaultLocation() throws Exception {
File basedir = resources.getBasedir("basic");
maven.executeMojo(basedir, "generateStubs");
assertFilesPresent(basedir, "target/mappings/Sample.json");
}
@Test
public void shouldGenerateWiremockFromStubsDirectory() throws Exception {
public void shouldGenerateWireMockFromStubsDirectory() throws Exception {
File basedir = resources.getBasedir("withStubs");
maven.executeMojo(basedir, "generateStubs", TestMavenRuntime.newParameter("contractsDir", "/src/test/resources/stubs"));
maven.executeMojo(basedir, "generateStubs", newParameter("contractsDir", "/src/test/resources/stubs"));
assertFilesPresent(basedir, "target/mappings/Sample.json");
}
@Test
public void shouldGenerateWiremockStubsInSelectedLocation() throws Exception {
public void shouldGenerateWireMockStubsInSelectedLocation() throws Exception {
File basedir = resources.getBasedir("basic");
maven.executeMojo(basedir, "generateStubs", TestMavenRuntime.newParameter("mappingsDir", "foo"));
maven.executeMojo(basedir, "generateStubs", newParameter("mappingsDir", "foo"));
assertFilesPresent(basedir, "target/foo/Sample.json");
}
@@ -47,4 +48,12 @@ public class PluginUnitTest {
"target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestSpec.groovy");
}
@Test
public void shouldGenerateContractTestsInDefaultLocation() throws Exception {
File basedir = resources.getBasedir("basic");
maven.executeMojo(basedir, "generateTests");
assertFilesPresent(basedir,
"target/generated-test-sources/accurest/io/codearte/accurest/tests/AccurestTest.java");
}
}

View File

@@ -0,0 +1,139 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-test-deps</artifactId>
<version>${it-plugin.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-test-deps</artifactId>
<version>${it-plugin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>io.codearte.accurest</groupId>
<artifactId>accurest-maven-plugin</artifactId>
<version>${it-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>generateTests</goal>
</goals>
</execution>
</executions>
<configuration>
<basePackageForTests>hello</basePackageForTests>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<dependencies>
<dependency>
<groupId>io.codearte.accurest</groupId>
<artifactId>stubs-assembly-descriptor</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>stubs</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>add-accurest-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-test-sources/accurest</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

View File

@@ -0,0 +1,12 @@
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,20 @@
package hello;
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}

View File

@@ -0,0 +1,19 @@
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
return new Greeting(counter.incrementAndGet(),
String.format(template, name));
}
}

View File

@@ -0,0 +1,21 @@
io.codearte.accurest.dsl.GroovyDsl.make {
priority 2
request {
method 'GET'
urlPath('/greeting') {
queryParameters {
parameter 'name': 'Something'
}
}
headers {
header 'Content-Type': 'application/json'
}
}
response {
status 200
body(
id: 1,
content: "Hello, Something!"
)
}
}

View File

@@ -0,0 +1,17 @@
io.codearte.accurest.dsl.GroovyDsl.make {
priority 2
request {
method 'GET'
url('/greeting')
headers {
header 'Content-Type': 'application/json'
}
}
response {
status 200
body(
id: 1,
content: "Hello, World!"
)
}
}

View File

@@ -0,0 +1,13 @@
package hello;
import com.jayway.restassured.module.mockmvc.RestAssuredMockMvc;
import org.junit.Before;
public class BaseAccurest {
@Before
public void setup() {
RestAssuredMockMvc.standaloneSetup(new GreetingController());
}
}