public static Result foo() {
// some previous code
WSRequestHolder request = WS.url(backendUrl); return async(
request.get().map(
new Function<WS.Response, Result>() {
public Result apply(WS.Response response) {
// some callback code to process the response
return ok(Json.toJson(someObject));
}
}
)
);
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({ WS.class })
public class ApplicationTest {
@Test
public void mockWsCall() {
mockStatic(WS.class);
Mockito.when(WS.url("http://www.google.com")).thenReturn(new MyRequestHolder("http://www.google.com"));
}
public static class MyRequestHolder extends WSRequestHolder{
public MyRequestHolder(String url) {
super(url);
System.out.println("my holder");
}
}
}
In your build.sbt add the following lines:
"org.powermock" % "powermock-core" % "1.5.4",
"org.powermock" % "powermock-module-junit4" % "1.5.4",
"org.powermock" % "powermock-api-mockito" % "1.5.4"
PowerMock is a nice alternative to mock Static behavior...
http://alots.wordpress.com
https://leanpub.com/playframeworknapratica
--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Nice!. But, even with the service isolated in a class, the access still is static.