2016-10-22 61 views
1

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

下面是我当前的代码:

控制器:

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

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

注:当我运行应用程序时,错误处理是按预期工作。

回答