Java 使用基于JUnit5平台的外部runner运行测试时出现UndefinedStepException

Java 使用基于JUnit5平台的外部runner运行测试时出现UndefinedStepException,java,cucumber,junit5,Java,Cucumber,Junit5,我已经围绕JUnit5平台编写了一个非常简单的包装器,用于运行由外部逻辑过滤的测试(它为path、packageId、filterFilePath提供数据——这些数据的处理被代码示例中的常量值所取代,以简化代码段) 以下是此包装器的主要逻辑: public static void main(String[] args) throws JsonProcessingException { String path = "D:\\repo\\cucumber-java-skeleton

我已经围绕JUnit5平台编写了一个非常简单的包装器,用于运行由外部逻辑过滤的测试(它为path、packageId、filterFilePath提供数据——这些数据的处理被代码示例中的常量值所取代,以简化代码段)

以下是此包装器的主要逻辑:

public static void main(String[] args) throws JsonProcessingException {
    String path = "D:\\repo\\cucumber-java-skeleton\\build\\libs";
    String[] packageIds = "io.cucumber.skeleton".split(",");
    String filterFilePath = "D:\\repo\\testrunner\\Tests\\Data\\DummyDataProject\\Java\\DummyJavaFilter.json";// contains JSON serialized list of correct test IDs

    ClassLoader contextLoader = TestsLoader.GetLoader(path);
    Thread.currentThread().setContextClassLoader(contextLoader);
    final Launcher launcher = LauncherFactory.create();

    List<TestResult> results = new ArrayList<TestResult>();
    launcher.execute(getFilteredTestPlan(launcher, filterFilePath),
            new TestRunnerExecutionListener(results));
    ObjectMapper mapper = new ObjectMapper();
    final String jsonResult = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(results);
    System.out.print("-==TEST RESULTS START==-");
    System.out.print(jsonResult);
    System.out.print("-==TEST RESULTS END==-");
    }
}
对于junit平台集成,以及使用可发现测试构建适当的jar包:

jar {
  from configurations.testCompileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
  from sourceSets.test.output
}
当添加步骤定义并按预期运行测试(使用默认的gradle测试任务)时,解决方案本身运行非常平稳。但是,当使用上面提供的代码运行时,它无法找到步骤定义,并且在io.Cumber.junit.platform.engine.UndefinedStepException中失败,即使存在步骤。将glue属性添加到@CucumberOptions也不能解决这个问题(尽管甚至不需要,因为步骤定义在同一个包中)

所以我已经花了很多时间在没有任何运气的情况下挖掘所有可用的资源,如果有任何帮助,我将不胜感激

我已经删除了所有错误处理和部分,该部分处理与问题无关的测试发现,来自此代码的测试ID作为输入被正确验证

更新: 当我将步骤定义添加到包含executor逻辑的同一个包中的类中时,它成功地发现了它,即使粘合设置指向一个完全不同的包,这些注释似乎被忽略,因为上面的代码发现了测试,即使这些注释被完全删除:

@RunWith(Cucumber.class)
@CucumberOptions(plugin = { "pretty" }, glue = "io.cucumber.skeleton")

.

因此,经过一些实验,我想出了一个肮脏的破解方法:

System.setProperty("cucumber.glue", String.join(",", packageIds));//packageIds has to contain all Step Definitions pacakges

这将添加系统属性,该属性稍后由代码处理,如文档中所述,它要么获取属性文件(我没有将其添加到Jar文件中,因为我对Java和Jar打包细节非常陌生),要么读取我添加的JVM中的系统属性。一个更成功的解决方案是从包含测试的目标包中获取这些属性,如果没有过多的搜索和大量的反射使用,我也无法做到这一点。理想的解决方案是Cucumber Runner和Junit平台之间的适当集成,这还没有到位,或者我不知何故错过了它的使用。任何更好的回复都将受到欢迎,并将被标记为正确的答案。

因此,经过一些实验,我想出了一个肮脏的破解方法:

System.setProperty("cucumber.glue", String.join(",", packageIds));//packageIds has to contain all Step Definitions pacakges
这将添加系统属性,该属性稍后由代码处理,如文档中所述,它要么获取属性文件(我没有将其添加到Jar文件中,因为我对Java和Jar打包细节非常陌生),要么读取我添加的JVM中的系统属性。一个更成功的解决方案是从包含测试的目标包中获取这些属性,如果没有过多的搜索和大量的反射使用,我也无法做到这一点。理想的解决方案是Cucumber Runner和Junit平台之间的适当集成,这还没有到位,或者我不知何故错过了它的使用。任何更好的答复将不胜感激,并将被标记为正确答案

System.setProperty("cucumber.glue", String.join(",", packageIds));//packageIds has to contain all Step Definitions pacakges