代码之家  ›  专栏  ›  技术社区  ›  Ran Biron

在单元测试中确定Jetbrains IntelliJ IDEA 8或9是否正在运行

  •  2
  • Ran Biron  · 技术社区  · 16 年前

    我需要知道,在单元测试的上下文中,Jetbrains IntelliJ idea是否是测试发起方,如果是,运行的是哪个版本(或者至少是“9”或更早)。

    我不介意做一些肮脏的把戏(反射、文件系统、环境……),但我确实需要这些来“开箱即用”(不需要每个开发人员都设置一些特殊的东西)。

    我尝试了一些反射,但是找不到唯一的标识符。

    你知道吗?

    1 回复  |  直到 16 年前
        1
  •  2
  •   Gerd Klima    16 年前

    public static boolean isIdeaRunningTheTest() {
       try {
         final Class<?> aClass = Class.forName("com.intellij.rt.execution.junit.JUnitStarter");
         } catch (ClassNotFoundException e) {
            return false;
         }
    
         return true;
    }
    

    在确定想法版本时。。。

    只要安装目录遵循安装程序中的标准(在windows上,我不知道它在Mac或Linux系统上的安装位置),下面的方法就可以工作。

    public static String getIdeaVersionTheDumbWay() {
        String result="unknown";
        final String binPath = System.getProperty("idea.launcher.bin.path");
        if (binPath.contains("IntelliJ IDEA")) {
            final String[] strings = binPath.split(System.getProperty("file.separator")+System.getProperty("file.separator"));
            for (String s : strings) {
                final int startIndex = s.indexOf("IntelliJ IDEA");
                if (startIndex >= 0) {
                    result= s.substring(startIndex + 14);
                }
    
            }
    
        }
    
        return result;
    }