0

I'm using JUnit and now I want to execute a Java program (main method) before running the tests.

I.e. in my project I have a package with a class which has a main method. This I want to run (perhaps in a separate process) before I run my tests because the classes under test will connect to his process via sockets.

How can I do that?

And at the end I want to kill this process of course.

1 Answer 1

1

You have almost answered yourself. You need to launch this process in separate thread using either Runtime.exec() (http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html) or some more sophisticated tool like so Apache Commons Exec http://commons.apache.org/proper/commons-exec/index.html

Method annotated with '@Before' or '@BeforeClass' annotation can be good place to do this. The best way would be to program extra helper class as singleton. This class will be responsible for launching the thread only in case it was not launched before, so you will have only one process for all the tests.


Edit: It should be something like:

 @BeforeClass
  public static void startProess() throws Exception {
    SomeProcess .getInstance().startIfNotRunning();
  }

public class SomeProcess {
  private Thread thread;
  private Process process;


  private static SomeProcess instance = new SomeProcess ();
  public static SomeProcess getInstance() {
    return instance;
  }

  public synchronized void startIfNotRunning() throws Exception {
      (...) 
      // check if it's not running and if not start
      (...)
      instance.start();
      (...)
  }

  public synchronized void stop() throws Exception {
      process.destroy()
  }

 private synchronized void start() throws Exception {
    thread = new Thread(new Runnable() {
        @Override
        public void run() {
           process = Runtime.exec("/path/yo/your/app");
        }

      });


    thread.start();

    // put some code to wait until the process has initialized (if it requires some time for initialization.

  }

}
Sign up to request clarification or add additional context in comments.

12 Comments

But in this way the program is executed as a thread and not as a process?
You will have a thread which will run separate process. By this I mean it creates new process in operating system. See java docs of Runtime.exec() . Runtime.exec() returns an instance of Process class which is connected to native process which is launched in operating system. You can use this returned instance of Process to communicated with it- you can read console output of this process for example.
How can I kill the process in the after clause in JUnit?
Store the instance of Process returned by Runtime.exe() (I edited the example according this). And later just call process.destroy(). I edited again to create stop() method. So on @AfterClass you call SomeProcess.getInstance().stop()
Thank you very much. Does this new process create a new JVM instance? Like launching the program from command line.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.