简体   繁体   English

使用fakeRequest播放2.5.x junit测试错误处理

[英]Play 2.5.x junit test error handling with fakeRequest

My program uses global error handling. 我的程序使用全局错误处理。 When i using fakeRequest() to test, it throws out exception instead of trigger my error handler. 当我使用fakeRequest()进行测试时,它抛出异常,而不是触发我的错误处理程序。 Am I missing some configuration? 我是否缺少某些配置? Or how can I test my global error handler. 或如何测试全局错误处理程序。

Below is my current code: 下面是我当前的代码:

Controller: 控制器:

public class MyController {
    public Result MyService() {
        if (true) throw new RuntimeException("exception");
    }
}

ErrorHandler: 的ErrorHandler:

@Singleton
public class MyErrorHandler extends DefaultHttpErrorHandler {

  @Inject
  public MyErrorHandler (Configuration configuration, Environment environment, OptionalSourceMapper sourceMapper, Provider<Router> routes) {
    super(configuration, environment, sourceMapper, routes);
  }

  @Override
  public CompletionStage<Result> onServerError(Http.RequestHeader request, Throwable exception) {
    return CompletableFuture.completedFuture(ok(exception.getMessage()));
  }
}

Test: 测试:

public class MyTest {
  @Test
  public void testMethod() {
    Result result = route(fakeRequest("GET", "/MyService"));
    assertEquals(OK, result.status());
  }
}

Note: When I run application, the error handling is working as expected. 注意:当我运行应用程序时,错误处理正在按预期方式工作。

The lack of error handler in the unit test flow is by design. 单元测试流程中缺少错误处理程序是设计使然。 It is meant to be like that. 就是那样。

With a simple router, you can get around this problem. 使用简单的路由器,您可以解决此问题。

This link might be of some help for both of these points: https://github.com/playframework/playframework/issues/2484 此链接可能对这两点都有帮助: https : //github.com/playframework/playframework/issues/2484

Not repeating the contents of the link here. 在此不重复链接的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM