让Junit重用Tomcat context.xml
我不是java专家,但现在我继承了一些我需要工作的代码。我的目标是运行源测试套件。测试所需的(创建)bean 需要一些环境设置。这些设置位于 tomcat-root-dir/conf/context.xml 中。 tomcat 使用这些环境设置来部署 Web 应用程序。
我想将 tomcat-root-dir/conf/context.xml 与 junit 一起重用,或者以其他方式使测试套件正常工作。是否有一些标准方法来解析 junit 的 context.xml ?如果您能帮我解决这个问题,我将不胜感激。
I am not an expert in java but right now I have inherited some code that I need to make work. My goal is to run the source test suite. The (creation of the) beans needed for the tests require some environment settings. These settings are in tomcat-root-dir/conf/context.xml. These environment settings are used by tomcat to deploy the web application.
I want to reuse tomcat-root-dir/conf/context.xml with junit, or otherwise make the testsuite work. Is there some standard way to parse context.xml for junit? I would appreciate if you could help me with this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一种解析 XML 的标准方法,因此您可以自由地读取您想要的任何文件并解析 XML。
我假设您想要对数据源或类似的东西进行 JNDI 查找。当然,问题是,如果您坚持这样做,您就会耦合到 Tomcat。
There's a standard way to parse XML, so you're free to read any file you wish and parse XML.
I'm assuming that you want a JNDI lookup for a data source or something like that. The problem, of course, would be that you're coupled to Tomcat if you persist along this line.
JUnit4提供了ExternalResource规则来处理资源:参见ExternalResource核心规则< /a>.
JUnit4 provides Rule ExternalResource to handle resource: See ExternalResource Core Rule.
TomcatJNDI 解决了这个问题。它可以处理Tomcat的配置文件并创建与Tomcat相同的JNDI环境,但无需启动服务器。因此,您可以在 JUnit 测试等 Tomcat 的 JNDI 环境中运行依赖于 Tomcat 的 JNDI 环境的类。
然后您的类可以像在 Tomcat 中运行一样查找数据源。
可以在此处找到有关 TomcatJNDI 的更多信息。
TomcatJNDI solves this problem. It can process Tomcat's configuration files and creates the same JNDI environment as Tomcat does, but without starting a server. So you can run classes with dependencies on Tomcat's JNDI environment in e. g. JUnit tests.
Then your classes can lookup the DataSource as when they would run in Tomcat.
More about TomcatJNDI can be found here.
我们采取了使用嵌入式 glassfish 服务器的方法。这不会重用 tomcat context.xml,但能够访问外部资源。
We have taken the approach of using an embedded glassfish server. This would not reuse the tomcat context.xml but will be able to access external resources.