代码之家  ›  专栏  ›  技术社区  ›  Ana Franco

如何创建黄瓜爪哇自定义格式化程序以获取黄瓜标签

  •  3
  • Ana Franco  · 技术社区  · 6 年前

    我有一个黄瓜项目,我想让项目中的所有标签都能够选择它们作为参数。

    我找到这个了 question Cucumber可以选择拿到标签,但我发现它已经不起作用了,于是我找到了另一个 question 我发现我需要一个自定义的格式化程序来获取我的标签,但是它是Ruby,我需要它来做Java,所以我发现了这个。 article 关于如何创建自定义格式化程序,但我发现这对CUKES版本有效,我正在使用IO版本。

    因此,我在cumber包中进行了搜索,并从cumber.runtime.formatter包中的jsonformatter副本创建了一个自定义格式化程序,下面是我的代码:

    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()));
        }    
    }
    

    我复制了我需要在项目中的lib文件夹中运行cumber的库,并尝试使用如下格式设置工具运行它:

    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"
    

    但是我得到一个类未找到异常:

    λ 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
    

    那么,我如何才能以一种被识别的方式创建这个格式化程序呢?或者至少从控制台获取Cucumber的标签列表?

    谢谢

    1 回复  |  直到 6 年前
        1
  •  1
  •   M.P. Korstanje    6 年前

    只要看一下你的代码,我认为没有什么问题。但是,您的命令似乎不包含 TagsFormatter 在类路径上。

    如果编译的源在 .\bin\ 确保包含该文件夹,即:

    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;.\bin\* cucumber.api.cli.Main -p "com.myproject.formatters.TagsFormatter:tags.txt"