是否可以在具有 JNDI 和 spring 上下文的 Jetty 容器中进行 运行 TestNG 集成测试?
Is it possible to run TestNG integration-tests in a Jetty container with JNDI's and spring context?
我在此处描述了 Maven + Failsafe + Jetty 配置:http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html。
我的本地开发 Jetty 配置在 XML 文件中定义,其中包含一些 JNDI,如下所述:https://www.eclipse.org/jetty/documentation/9.3.x/using-jetty-jndi.html。
是否可以在 Jetty 容器(环境)内进行 运行 TestNG 测试,以便我可以访问所有 JNDI 和 Spring 上下文我的 war(应用程序)有权访问?
在我看来,测试总是在某种需要自己设置的孤立环境中执行,但如果是这样的话,首先对 运行 Jetty 来说似乎毫无意义。也许除了那些我需要进行一些 HTTP 调用来测试我的 REST 端点的情况,但事实并非如此。
经过一番研究,我终于找到了 运行 我的测试方法:
- JNDI 上下文 (
InitialContext
) 从我的 Jetty 配置
配置 XML.
- Spring 包含一些 bean 的上下文,需要那些 JNDI 资源进行初始化。
我的目标是重用现有的 Web 容器环境。由于我在 "in-container tests" 上没有成功,而不是在预集成测试阶段 运行ning Jetty,我决定编写自己的 ContextLoader
for a @ContextConfiguration
.
实现
这是我的解决方案的总体思路:
public class ITestContextLoader extends GenericXmlWebContextLoader {
@Override
protected void loadBeanDefinitions(...) {
// Initial Context configuration
Resource jettyXml = Resourse.newSystemResource("jetty-env.xml");
InputStream resourceStream = jettyXml.getInputStream();
XmlConfiguration xmlConfig = new XmlConfiguration(resourceStream);
xmlConfig.configure(); // for some reason it's not only returns
// instance of configured context, but also
// populates InitialContext with JNDI resources
// Bean definitions loading
...
}
}
这不是我一开始看到或想做的方式(当我发布问题时),而是我目前想到的最接近的解决方案。也许不是最好的,所以我当然会感谢替代方案、建议和指示。
我在此处描述了 Maven + Failsafe + Jetty 配置:http://maven.apache.org/surefire/maven-failsafe-plugin/usage.html。
我的本地开发 Jetty 配置在 XML 文件中定义,其中包含一些 JNDI,如下所述:https://www.eclipse.org/jetty/documentation/9.3.x/using-jetty-jndi.html。
是否可以在 Jetty 容器(环境)内进行 运行 TestNG 测试,以便我可以访问所有 JNDI 和 Spring 上下文我的 war(应用程序)有权访问?
在我看来,测试总是在某种需要自己设置的孤立环境中执行,但如果是这样的话,首先对 运行 Jetty 来说似乎毫无意义。也许除了那些我需要进行一些 HTTP 调用来测试我的 REST 端点的情况,但事实并非如此。
经过一番研究,我终于找到了 运行 我的测试方法:
- JNDI 上下文 (
InitialContext
) 从我的 Jetty 配置 配置 XML. - Spring 包含一些 bean 的上下文,需要那些 JNDI 资源进行初始化。
我的目标是重用现有的 Web 容器环境。由于我在 "in-container tests" 上没有成功,而不是在预集成测试阶段 运行ning Jetty,我决定编写自己的 ContextLoader
for a @ContextConfiguration
.
这是我的解决方案的总体思路:
public class ITestContextLoader extends GenericXmlWebContextLoader {
@Override
protected void loadBeanDefinitions(...) {
// Initial Context configuration
Resource jettyXml = Resourse.newSystemResource("jetty-env.xml");
InputStream resourceStream = jettyXml.getInputStream();
XmlConfiguration xmlConfig = new XmlConfiguration(resourceStream);
xmlConfig.configure(); // for some reason it's not only returns
// instance of configured context, but also
// populates InitialContext with JNDI resources
// Bean definitions loading
...
}
}
这不是我一开始看到或想做的方式(当我发布问题时),而是我目前想到的最接近的解决方案。也许不是最好的,所以我当然会感谢替代方案、建议和指示。