问题
I have an unit test like this:
@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");
}
});
}
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.
回答1:
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`
will do the trick to have the JVM (running your Play server) listening for one debug connection.
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
- launch
testand debug (in case you've unit test for your controller) - test your app by hand (otherwise)
EDIT
Related to running the test in eclipse, I cannot see how to do since it relies on the classpath and probably some configuration.
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.
While starting attach you debugger (remote app) to the jvm in order the walk the code.
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.
来源:https://stackoverflow.com/questions/10859064/how-to-debug-play-2-unit-test-for-server-side