Search code examples
javatestingjunitautomated-testssikuli

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


I have a Junit / Silkuli test that runs on windows 7 and is dependent on an external program already running.

How can I check if the external program is running from inside the test?


Solution

  • You can run command "tasklist" from within java and look at the returned list to see if your program is in the list. something like:

    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
    

    and get the output from a command using a method such as:

    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;
    }