How to create a cucumber-java custom formatter to get cucumber tags

Ana*_*nco 5 custom-formatting cucumber-jvm

I have a cucumber project and I want to get al the tags in the project to be able to choose them as parameters.

I found this question where cucumber had an option to get the tags but I found that doesn't work anymore, then I found this other question where I found I need a custom formatter to get my tags, but it is for ruby, and I need it for Java, so then I found this article on how to create a custom formatter, but I found that this worked for the cukes version and I'm using the io one.

So I searched inside the cucumber packages and created a custom formatter from a copy of the JSONFormatter inside the package cucumber.runtime.formatter, here is my code:

import cucumber.api.TestCase;
import cucumber.api.event.*;
import cucumber.api.formatter.Formatter;
import cucumber.api.formatter.NiceAppendable;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import gherkin.pickles.PickleTag;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TagsFormatter implements Formatter {

    private String currentFeatureFile;
    private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
    private final NiceAppendable out;

    private List<String> tags = new ArrayList<>();


    private EventHandler<TestCaseStarted> caseStartedHandler = this::handleTestCaseStarted;

    private EventHandler<TestRunFinished> runFinishedHandler = event -> finishReport();

    public TagsFormatter(Appendable out) {
        this.out = new NiceAppendable(out);
    }

    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseStarted.class, caseStartedHandler);
        publisher.registerHandlerFor(TestRunFinished.class, runFinishedHandler);
    }

    private void handleTestCaseStarted(TestCaseStarted event) {
        if (currentFeatureFile == null || !currentFeatureFile.equals(event.testCase.getUri())) {
            currentFeatureFile = event.testCase.getUri();
            collectTags(event.testCase);
        }
    }

    private void finishReport() {
        out.append(gson.toJson(tags));
        out.close();
    }

    private void collectTags(TestCase testCase) {
        testCase.getTags();
        tags.addAll(testCase.getTags()
                .stream()
                .map(PickleTag::getName)
                .collect(Collectors.toList()));
    }    
}
Run Code Online (Sandbox Code Playgroud)

I copied the libraries I need to run cucumber in a lib folder inside my project and tried running it using my formatter like this:

java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
Run Code Online (Sandbox Code Playgroud)

But Im getting a class not found exception:

? java -cp .\lib\cucumber-core-2.4.0.jar;.\lib\gherkin-5.0.0.jar;.\lib\cucumber-java-2.4.0.jar;.\lib\cucumber-jvm-deps-1.0.6.jar cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"
Exception in thread "main" cucumber.runtime.CucumberException: Couldn't load plugin class: com.myproject.formatters.TagsFormatter
        at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:181)
        at cucumber.runtime.formatter.PluginFactory.pluginClass(PluginFactory.java:166)
        at cucumber.runtime.formatter.PluginFactory.getPluginClass(PluginFactory.java:223)
        at cucumber.runtime.formatter.PluginFactory.isFormatterName(PluginFactory.java:201)
        at cucumber.runtime.RuntimeOptions$ParsedPluginData.addPluginName(RuntimeOptions.java:471)
        at cucumber.runtime.RuntimeOptions.parse(RuntimeOptions.java:157)
        at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:115)
        at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:108)
        at cucumber.runtime.RuntimeOptions.<init>(RuntimeOptions.java:100)
        at cucumber.api.cli.Main.run(Main.java:31)
        at cucumber.api.cli.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: com.myproject.formatters.TagsFormatter
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at cucumber.runtime.formatter.PluginFactory.loadClass(PluginFactory.java:174)
        ... 10 more
Run Code Online (Sandbox Code Playgroud)

So, how can I create this formatter in a way it is recognized? or at least get the tags list from cucumber from console?

Thanks