【发布时间】:2019-02-05 16:47:20
【问题描述】:
我有一个 Gherkin 执行器,用于执行我的功能文件。我想做的是从另一个 jar 添加一个 StepDefinition 文件。用户可以通过我已经编写的步骤 def 使用我的项目,但他也可以从他自己的 jar 文件中添加自定义定义。
目前我有一个 JavaClassLoader,我从我的 jar 中加载我的类并在我的 main 中使用它
public class JavaClassLoader<C> extends ClassLoader {
public C LoadClass(String directory, String classpath, Class<C> parentClass) throws ClassNotFoundException {
File pluginsDir = new File(System.getProperty("user.dir") + directory);
for (File jar : pluginsDir.listFiles()) {
try {
ClassLoader loader = URLClassLoader.newInstance(
new URL[] { jar.toURL() },
getClass().getClassLoader()
);
Class<?> clazz = Class.forName(classpath, true, loader);
Class<? extends C> newClass = clazz.asSubclass(parentClass);
// Apparently its bad to use Class.newInstance, so we use
// newClass.getConstructor() instead
Constructor<? extends C> constructor = newClass.getConstructor();
return constructor.newInstance();
} catch (ClassNotFoundException e) {
// There might be multiple JARs in the directory,
// so keep looking
continue;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
throw new ClassNotFoundException("Class " + classpath
+ " wasn't found in directory " + System.getProperty("user.dir") + directory);
}
}
JavaClassLoader<AbstractStepDefs> loader = new JavaClassLoader<AbstractStepDefs>();
loader.LoadClass("/", "stepDef.dynamicClass", AbstractStepDefs.class);
问题是 Cucumber 无法读取我在另一个 jar 中编写的方法。有没有办法使用项目中没有的 step def 文件?
【问题讨论】:
标签: java cucumber cucumber-jvm gherkin cucumber-java