简体   繁体   English

Intellij:如何在文件夹中运行所有main()方法?

[英]Intellij: How to run all main() methods in a folder?

In IntelliJ there is a feature that runs all unit tests in a folder. 在IntelliJ中,有一项功能可以运行文件夹中的所有单元测试。 Is there any possibility to run all main() methods in the same way? 是否有可能以相同的方式运行所有main()方法?

Not that I am aware of. 不是我知道的。

Workaround with wrapper class: 包装类的解决方法:

Create a class with a main method and call every main method in that method. main方法创建一个类,并调用该方法中的每个main方法。

If these classes with main methods changes a lot, you could use this Reflection Library with the following code to scan for classes with a main method: 如果这些具有main方法的类发生了很大变化,则可以将此反射库与以下代码一起使用,以扫描具有main方法的类:

 Reflections reflections = new Reflections("your.package.with.main.classes");

 Set<Class<? extends Object>> allClasses = 
     reflections.getSubTypesOf(Object.class);

The allClasses set contains all classes in that package. allClasses集包含该软件包中的所有类。

The following code would filter for classes that have a main method: 以下代码将筛选具有main方法的类:

Set<Class> mainClasses = allClasses.stream()
  .filter(clazz -> 
    Arrays.stream(clazz.getMethods())
     .anyMatch(method -> 
        method.getName().equals("main")))
   .collect(Collectors.toSet());

Calling the main method should not be problem anymore. 调用main方法应该不再是问题。

PS: A filtering for static and public modifier would be a good idea as well. PS:对staticpublic修饰符进行过滤也是一个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何为子目录中的所有源文件运行 unittest.main()? - Python: How to run unittest.main() for all source files in a subdirectory? 在 IntelliJ 中运行除 JUnit 类别之外的所有测试 - Run all tests except for a JUnit Category in IntelliJ 如何在Visual Studio中的特定文件夹中运行单个测试或所有测试? - How to run a single test or all tests in a specific folder in Visual Studio? 如何在Intellij中使用junit自动导入测试方法 - How to auto import methods for testing with junit in Intellij 运行单个Grails单元/集成测试将在Intellij上运行所有测试 - Run single Grails unit/integration test will run all the tests on Intellij intelliJ 如何运行 gradle 任务作为测试 - How can intelliJ run gradle task as test 除了使用集成测试运行器在IntelliJ IDEA项目中以“IntegrationTest”结尾的所有JUnit单元测试之外,我该如何运行? - How can I run all JUnit unit tests except those ending in “IntegrationTest” in my IntelliJ IDEA project using the integrated test runner? UI5-如何在没有allTests文件的情况下运行Karma,从文件夹运行所有测试 - UI5 - how to run Karma without allTests file, running all tests from the folder Ruby:如何用一个命令运行文件夹中的所有单元测试? - Ruby: How to run all unit tests that I have in a folder with a single command? 如何让 Pycharm 从测试文件夹递归运行所有 python 单元测试 - How to make Pycharm run all python unit tests recursively from tests folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM