猿问

返回类对象数组的方法读取文件

我使用 JUnit 来启动保存@SuiteClasses参数的主测试类。此参数包含类对象数组,即

@SuiteClasses({Test0.class, Test1.class, Test2.class})

我想将这些类名称(Test0、Test1、[..])保存在外部文件中(我不在乎它是否是 XML、CSV、TXT 等),这些文件将以任何方式保存所述名称 - 是否无关紧要它由逗号、换行符或新的 XML 对象分隔。我的理想方法应该可以这样使用

@SuiteClasses(methodReadingFileAndReturningClassObjs)

有可能吗?我怎样才能做到这一点?


qq_花开花谢_0
浏览 149回答 1
1回答

茅侃侃

我意识到TestSuite我想到的类是 JUnit 3 工件,它不再存在于 JUnit 4 中。您可以做的是扩展Suite跑步者以满足您的需求(双关语)。public class FilelistSuite extends Suite {&nbsp; &nbsp; public FilelistSuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {&nbsp; &nbsp; &nbsp; &nbsp; super(klass, loadFromFile(klass));&nbsp; &nbsp; }&nbsp; &nbsp; private static Class<?>[] loadFromFile(Class<?> klass) throws InitializationError {&nbsp; &nbsp; &nbsp; &nbsp; // get annotation&nbsp; &nbsp; &nbsp; &nbsp; SuiteclassesFile annotation = klass.getAnnotation(SuiteclassesFile.class);&nbsp; &nbsp; &nbsp; &nbsp; if (annotation == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new InitializationError(String.format("class '%s' must have a SuiteclassesFile annotation", klass.getName()));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return fromFile(annotation.filename());&nbsp; &nbsp; &nbsp; &nbsp; } catch (RuntimeException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new InitializationError(e.getCause());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // read file to extract test class names&nbsp; &nbsp; private static final Class<?>[] fromFile(String filename) throws RuntimeException {&nbsp; &nbsp; &nbsp; &nbsp; try (Stream<String> lines = Files.lines(Paths.get(filename))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .map(FilelistSuite::forName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toArray(Class[]::new);&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException ioe) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(ioe);&nbsp; &nbsp; &nbsp; &nbsp; } catch (RuntimeException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e.getCause());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // wrap Class.forName to be able to use it in the Stream&nbsp; &nbsp; private static final Class<?> forName(String line) throws RuntimeException {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Class.forName(line);&nbsp; &nbsp; &nbsp; &nbsp; } catch (ClassNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // new annotation for your test suite&nbsp; &nbsp; public @interface SuiteclassesFile {&nbsp; &nbsp; &nbsp; &nbsp; public String filename();&nbsp; &nbsp; }}现在你应该能够用@RunWith(FilelistSuite.class)@SuiteclassesFile(filename="/path/to/your/file")class YourTestClass {}我还没有真正尝试过这个,但它应该只需要很小的调整。实际上,由于您的问题的标题只是“返回类对象数组的方法读取文件”-该fromFile方法就是这样做的。
随时随地看视频慕课网APP

相关分类

Java
我要回答