我在Play 2.6上,使用Java
我的控制器返回:
public Result xml() {
return Results.ok(new ByteArrayInputStream("<someXml />".getBytes()));
}
我想在测试中解析结果:
Result result = new MyController().xml();
play.test.Helpers.contentAsString(result)
抛出
failed: java.lang.UnsupportedOperationException: Tried to extract body from a non strict HTTP entity without a materializer, use the version of this method that accepts a materializer instead
如何检索测试中输入流发出的结果内容?
答案 0 :(得分:3)
正如异常消息所述,由于您的结果是流实体,因此请使用Materializer
的版本contentAsString
。以下是Play存储库中HelpersTest.java
使用该方法的示例:
@Test
public void shouldExtractContentAsStringFromAResultUsingAMaterializer() throws Exception {
ActorSystem actorSystem = ActorSystem.create("TestSystem");
try {
Materializer mat = ActorMaterializer.create(actorSystem);
Result result = Results.ok("Test content");
String contentAsString = Helpers.contentAsString(result, mat);
assertThat(contentAsString, equalTo("Test content"));
} finally {
Future<Terminated> future = actorSystem.terminate();
Await.result(future, Duration.create("5s"));
}
}
答案 1 :(得分:0)
自带kka 2.6的Play 2.8起,“ ActorMaterializer”已被弃用。这是获得“材料化器”的方法
Materializer mat = Materializer.matFromSystem(actorSystem);