All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.carousell.cucumber2junit.CodeGenerationMojo Maven / Gradle / Ivy

Go to download

Generates plain JUnit tests from Gherkin feature files. This is useful when you want to execute Cucumber tests in an environment which does not allow custom JUnit runners, e.g. the AWS Device Farm.

The newest version!
package io.github.carousell.cucumber2junit;

import io.github.carousell.cucumber2junit.model.Feature;
import java.util.List;
import java.util.Map;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Mojo for the Maven plugin. */
@Mojo(name = "generate")
public class CodeGenerationMojo extends AbstractMojo {

  private static final Logger LOG = LoggerFactory.getLogger(CodeGenerationMojo.class);

  @Parameter(defaultValue = "${project}", readonly = true, required = true)
  private MavenProject project;

  @Parameter(defaultValue = "${project.basedir}/src/test/resources/features")
  private String featuresSourceDirectory;

  @Parameter(defaultValue = "${project.basedir}/src/test/java")
  private String javaOutputDirectory;

  @Parameter(defaultValue = "features")
  private String featuresClasspath;

  @Parameter(defaultValue = "/tmp/features")
  private String featuresOutputDirectory;

  @Parameter(required = true)
  private String packageName;

  @Parameter(required = true)
  private String stepsPackageName;

  @Parameter private String templatePath;

  @Parameter(property = "tags")
  private String[] tags;

  @Parameter private Map additionalPlaceholders;

  @Parameter(required = true)
  private String awsDynamoAccessKey;

  @Parameter(required = true)
  private String awsDynamoSecretKey;

  @Parameter(defaultValue = "us-east-1")
  private String awsRegion;

  private TagsResolver tagsResolver;
  private FeatureParser featureParser;
  private JUnitTestGenerator codeGenerator = new JUnitTestGenerator();

  public void execute() throws MojoExecutionException {
    tagsResolver = new AWSDynamoDBTagsResolver(awsDynamoAccessKey, awsDynamoSecretKey, awsRegion);
    featureParser = new FeatureParser(tagsResolver);

    LOG.info("Generating JUnit test classes...");
    LOG.info("featuresSourceDirectory = {}", featuresSourceDirectory);
    LOG.info("javaOutputDirectory = {}", javaOutputDirectory);
    LOG.info("featuresClasspath = {}", featuresClasspath);
    LOG.info("packageName = {}", packageName);
    LOG.info("stepsPackageName = {}", stepsPackageName);
    LOG.info("templatePath = {}", templatePath);
    LOG.info("tags = {}", tags);
        codeGenerator.generateCucumberMain(javaOutputDirectory, packageName);
        List features = featureParser.parseFeatures(featuresSourceDirectory, tags);
        codeGenerator.generateJUnitTests(
            features,
            featuresSourceDirectory,
            javaOutputDirectory,
            featuresOutputDirectory,
            featuresClasspath,
            packageName,
            stepsPackageName,
            templatePath,
            additionalPlaceholders);
        LOG.info(
            "Publishing scenario ids {} as Maven property {}",
            featureParser.getScenarioIds(),
            "scenarioIds");
        project.getProperties().put("scenarioIds", String.join(",",
     featureParser.getScenarioIds()));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy