如何测试不同虚拟机上 2 个或更多客户端之间的交互?
我需要在完整的端到端测试中测试两个或多个 Java 客户端之间的交互。
理想情况下,如果没有 GUI,它应该类似于(Junit 语法):
@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);
}
上面的代码不是我想要的,因为它使“应用程序”在同一虚拟机上运行。
我自己想到的解决方案是编写一个假 main 来实例化 secondaryApps 并将其所有输出写入文件中,通过系统调用启动它,然后检查结果,但这似乎是一个真正的杀伤力。此外,使用这样的策略,测试 GUI 也会变得更加困难(如果不是不可能的话)。
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
进程:
一种解决方案是使用Runtime.exec启动另一个进程,这将允许您从启动该进程的进程中监视该进程。这个新进程将在另一个 JVM 上运行。相当于叉子。
在 Java 中管理另一个进程时需要处理线程,有许多注意事项。
本教程应该有所帮助:当 Runtime.exec 时() 不会
通信:
有几种类型的方法可以让多个进程相互通信。如果您使用 Runtime.exec,您可以通过每个程序的输入流和输出流进行通信。
这非常慢,更好的解决方案是使用套接字直接在每个进程之间进行通信。
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.