Java/Junit : how to test if an external program/process/application is running?

标签 java testing junit automated-tests sikuli

我有一个在 Windows 7 上运行的 Junit/Silkuli 测试,并且依赖于已经运行的外部程序。

如何检查外部程序是否正在测试内部运行?

最佳答案

您可以从 java 中运行命令“tasklist”并查看返回的列表以查看您的程序是否在列表中。像这样:

String program = "eclipse.exe";   //or any other process
String listOfProcesses = getCommandOutput("tasklist");
 if (listOfProcesses == null || listOfProcesses.isEmpty()) {
        System.err.println("Unable to automatically determine if " + program + " is running");
    } else {
        if (listOfProcesses.contains(program)) {
            System.out.println(program + " is running!");
        } else {
            System.out.println(program + " is not running!");
        }
    }//else: process list can be retrieved

并使用以下方法获取命令的输出:

public static String getCommandOutput(String command)  {
    String output = null;       //the string to return

    Process process = null;
    BufferedReader reader = null;
    InputStreamReader streamReader = null;
    InputStream stream = null;

    try {
        process = Runtime.getRuntime().exec(command);

        //Get stream of the console running the command
        stream = process.getInputStream();
        streamReader = new InputStreamReader(stream);
        reader = new BufferedReader(streamReader);

        String currentLine = null;  //store current line of output from the cmd
        StringBuilder commandOutput = new StringBuilder();  //build up the output from cmd
        while ((currentLine = reader.readLine()) != null) {
            commandOutput.append(currentLine + "\n");
        }

        int returnCode = process.waitFor();
        if (returnCode == 0) {
            output = commandOutput.toString();
        }

    } catch (IOException e) {
        System.err.println("Cannot retrieve output of command");
        System.err.println(e);
        output = null;
    } catch (InterruptedException e) {
        System.err.println("Cannot retrieve output of command");
        System.err.println(e);
    } finally {
        //Close all inputs / readers

        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                System.err.println("Cannot close stream input! " + e);
            }
        } 
        if (streamReader != null) {
            try {
                streamReader.close();
            } catch (IOException e) {
                System.err.println("Cannot close stream input reader! " + e);
            }
        }
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                System.err.println("Cannot close reader! " + e);
            }
        }
    }
    //Return the output from the command - may be null if an error occured
    return output;
}

关于Java/Junit : how to test if an external program/process/application is running?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16316950/

相关文章:

java - ViewPager 中的 Espresso RecyclerView

java - Guice:Junit 测试类中的创建异常

java - 当输入来自多个文件时如何使用参数化 junit?

java - 如何测试实用程序项目的 final方法和静态方法?

java - 将 org.bouncycaSTLe.jce.PKCS10CertificationRequest 更新为 org.bouncycaSTLe.pkcs.PKCS10CertificationRequest;

iphone - TestFlight 找不到构建

wcf - 推荐用于测试 SOAP 和 REST 服务的测试框架?

java - Android中使用KSOAP访问Webservice不起作用

java - 将大型 PNG 图像导出到使用 Xcode 编译的 java 可执行文件中

java - 如何在Hibernate中查询嵌入对象?