本文介绍了播放2.4:如何在单元测试期间禁用路由文件加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在将Play 2.4(Java)与InjectedRoutesGenerator和Guice模块配合使用,以配置各种依赖项.但是在单元测试期间,FakeApplication试图通过注入器从路由文件中加载所有Controller,由于某些依赖关系在单元测试环境中不可用,因此其中一些失败.

Background: I am using Play 2.4 (Java) with InjectedRoutesGenerator and a Guice module to configure various dependencies. But during unit tests, the FakeApplication is trying to load all the Controllers from routes file through the injector and some of them are failing due to external dependencies not available in the unit test environment.

在从play.test.WithApplication扩展的单元测试期间,如何禁用默认路由文件处理?或者如何用自定义路由文件替换默认路由?

我尝试使用 play.http.路由器配置选项覆盖此处引用的内容,但是我尝试执行的任何操作均出现Router not found错误.显然我在犯一些错误,我不确定在哪里.

I tried to use the play.http.router config option override referenced here, but I get Router not found error with anything I tried. Obviously I am making some mistake, I am not sure where.

我不太了解.除了routes之外的路由文件也不会被编译.

I am not quite understanding the link between the my.application.Router and conf/my.application.routes referenced in the config reference. Route files other than routes do not get compiled either.

推荐答案

我在这里回答自己的问题.在花了更多时间玩Play源代码之后,我弄清了routes文件和生成的Router类之间的连接.希望对别人有帮助.

I am answering my own question here. After spending some more time with Play source code, I figured out the connection between the routes file and the generated Router class. Hope it helps someone else.

Play的路由编译器任务将编译以.routes结尾的conf文件夹中的所有文件以及默认的routes文件.生成的类名称始终为Routes,但是程序包名称取决于文件名.如果文件名是routes(默认路由文件),则已编译的类放在router包中,因此完全限定的类名是router.Routes(这是play.http.router的默认值).

Play's route compiler task compiles all files in conf folder ending with .routes as well as the default routes file. Generated class name is always Routes, but the package name depends on the file name. If the file name is routes (the default routes file), compiled class is placed in router package, so the fully qualified class name is router.Routes (which is the default value for play.http.router).

对于所有其他路由文件,RouteCompiler通过从文件名中删除.routes来导出程序包名称.因此,对于my.test.routesplay.http.router的值应为my.test.Routes.

For all other route files, RouteCompiler derives the package name by dropping the .routes from the file name. So for my.test.routes, play.http.router value should be my.test.Routes.

这是我的测试的基类,带有自定义路由器和数据库配置元素.

Here is the base class for my tests, with custom router and db configuration elements.

public class MyTestBase extends WithApplication {
    @Override
    protected Application provideApplication() {
        Application application = new GuiceApplicationBuilder()
                .configure("db.default.driver", "org.h2.Driver")
                .configure("db.default.url", "jdbc:h2:mem:play")
                .configure("play.http.router", "my.test.Routes")
                .build();
        return application;
    }
}

这篇关于播放2.4:如何在单元测试期间禁用路由文件加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 09:05