views:

20

answers:

1

I need to test the interactions between 2 ore more java clients in a complete end-to-end test.

Without the GUIs ideally it should be something similar to (Junit syntax):


@Test public void EndToEndTest(){ App firstApp = new App(); App secondApp = new App(); String msg = "something"; firstApp.send(msg); //this method will magically send msg to secondApp String msgReceived = secondApp.getLastMsg(); AssertEquals(msgReceived, msg); }

The code above is not what I want because it makes the "apps" run on the same VM.

The solution that I was able to think on my own was to write a fake main that instantiates the secondApps and writes all its output on a file, launch it with a system call and then check the result but it seems to be a real overkill. Moreover with a strategy like that it would be harder (if not impossible) to test also the GUI.

Thank you for your help.

A: 

Process:

One solution would be to use Runtime.exec to launch another process which would allow you to monitor that process from within the process which launched it. This new process will run on another JVM. It's equivalent to fork.

There are many caveats which deal with threading that you need to handle for managing another process within Java.

This tutorial should help: When Runtime.exec() won't

Communication:

There are a few types of ways you can have multiple processes communicate with each other. If you are using the Runtime.exec you can communicate through both the in and out streams of each program.

This is pretty slow and the much better solution would be to use sockets to communicate between each process directly.

David Young