gpt4 book ai didi

playframework - Play 2.4 : How do I disable routes file loading during unit tests?

转载 作者:行者123 更新时间:2023-12-03 23:21:41 25 4
gpt4 key购买 nike

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

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

我尝试使用 play.http.router config option override referenced here ,但我得到 Router not found我尝试过的任何事情都出错。显然我犯了一些错误,我不确定在哪里。

我不太了解 config reference 中引用的 my.application.Router 和 conf/my.application.routes 之间的链接。 .路由 routes 以外的文件也不要编译。

最佳答案

我在这里回答我自己的问题。在花更多时间研究 Play 源代码之后,我发现了路由文件和生成的 Router 类之间的联系。希望它可以帮助别人。

Play的路由编译任务编译conf中的所有文件以 .routes 结尾的文件夹以及默认的 routes文件。生成的类名总是 Routes ,但包名取决于文件名。如果文件名是routes (默认路由文件),编译后的类放在router包,所以完全限定的类名是 router.Routes (这是 play.http.router 的默认值)。

对于所有其他路由文件,RouteCompiler 通过删除 .routes 来派生包名称。从文件名。所以对于 my.test.routes , play.http.router值应为 my.test.Routes .

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

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

关于playframework - Play 2.4 : How do I disable routes file loading during unit tests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com