如何调试服务器端的Play 2单元测试 [英] How to debug Play 2 unit test for server side
问题描述
我有这样的单元测试:
@Test
public void callRegisterByForm() {
// AnyContentAsJson anyContentAsJson = null;
running(fakeApplication(), new Runnable() {
@Override
public void run() {
Map<String, String> map = new HashMap<String, String>();
map.put("phoneNumber", "+5103978860");
map.put("countryCode", "us");
Result result = routeAndCall(fakeRequest("POST", "/register").withFormUrlEncodedBody(map));
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("application/json");
JsonNode jsonResult = Json.parse(contentAsString(result));
assertThat(jsonResult.get(Config.DEV_REG_STATUS_PARAM).asText()).isEqualTo("OK");
assertThat(jsonResult.get(Config.DEV_PHONE_NUMBER_PARAM).asText()).isEqualTo("+5103978860");
}
});
}
如何测试服务器的控制器?我尝试设置远程Java应用程序并在控制台上使用播放调试运行",但仍然无法右键单击此测试用例以运行,并且让服务器的控制器在断点处停止 >.
How do I test the server's controller? I tried to setup a remote java application and use 'play debug run' on console but still not able right click on this test case to run and have the server's controller stop at breakpoints.
这是我右键单击测试用例时的图像.它要求我选择要使用的启动器.
This is the image when I right click a test case. It asks me to select a launcher to use.
推荐答案
MMMh,因为我正在使用TypeSafe堆栈,所以我无法访问play
可执行文件.
MMMh since I'm using the TypeSafe stack I don't have access to the play
executable.
但是,我知道一种在调试模式下启动播放服务器的方法. 实际上,我是在自己的博客此处.
However, I know a way to start the play server in debug mode. Actually I wrote it on my blog here.
`export SBT_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9999`
可以使JVM(运行您的Play服务器)侦听一个调试连接.
will do the trick to have the JVM (running your Play server) listening for one debug connection.
说完之后,您设置了一个连接到localhost:9999
After what, you've said it, you setup a remote application that connect to localhost:9999
在播放控制台中,您可以选择
In the play console, you'll have the option to
- 启动
test
并进行调试(以防您已经对控制器进行单元测试) - 手动测试您的应用(否则)
- launch
test
and debug (in case you've unit test for your controller) - test your app by hand (otherwise)
编辑
与在Eclipse中运行测试有关,我看不到该怎么做,因为它依赖于类路径和可能的某些配置.
Related to running the test in eclipse, I cannot see how to do since it relies on the classpath and probably some configuration.
但是,假设在包test.accounts
下的测试类路径中有一个名为MyTest
的测试,则可以执行play debug test-only test.accounts.MyTest
.这将仅执行MyTests的测试.
However what could be done is to execute play debug test-only test.accounts.MyTest
, assuming you've a test called MyTest
in the test classpath under the package test.accounts
. This will execute only the MyTests's tests.
在开始将调试器(远程应用程序)附加到jvm时,以便遍历代码.
While starting attach you debugger (remote app) to the jvm in order the walk the code.
另一种方法可能是:
* play debug
在一个控制台中,
*附加调试器
*打开另一个控制台以根据需要多次执行test-only test.accounts.MyTest
.
Another way might be :
* play debug
in one console,
* attach the debugger
* open an another console to execute test-only test.accounts.MyTest
as many times as you want.
这篇关于如何调试服务器端的Play 2单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!