使用fakeRequest播放2.5.x junit测试错误处理 [英] Play 2.5.x junit test error handling with fakeRequest

查看:92
本文介绍了使用fakeRequest播放2.5.x junit测试错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序使用全局错误处理.当我使用fakeRequest()进行测试时,它抛出异常,而不是触发我的错误处理程序. 我是否缺少某些配置?或如何测试全局错误处理程序.

My program uses global error handling. When i using fakeRequest() to test, it throws out exception instead of trigger my error handler. Am I missing some configuration? Or how can I test my global error handler.

下面是我当前的代码:

控制器:

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()));
  }
}

测试:

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.

此链接对于这两点都可能有所帮助: https://github.com. com/playframework/playframework/issues/2484

This link might be of some help for both of these points: https://github.com/playframework/playframework/issues/2484

此处不再重复链接的内容.

Not repeating the contents of the link here.

这篇关于使用fakeRequest播放2.5.x junit测试错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆