tomcat - 我的 JUnit 测试如何找到我的 Tomcat 数据源?

标签 tomcat jax-rs jersey-2.0 httpserver jersey-test-framework

我正在尝试测试我的 REST 服务。我正在使用 Jersey2 的 JerseyTest 和提供的 JDK HttpServer 框架。我需要测试来访问在 META-INF/context.xml 中定义的数据源。服务部署在 Tomcat 上。

如何将 ContextConfig 添加到 ResourceConfig 使其正常工作?

这是设置代码,目前不起作用。上下文似乎没有注册。

@Override
protected Application configure() {
    ResourceConfig rc = new ResourceConfig(OfficeSpaceService.class);
    rc.property("contextConfigLocation", "src/main/webapp/META-INF/context.xml");

    return rc;
}

这是测试代码:

@Test
public void getSpacesTest() {
    Response response = 
        target("v1/space/")
        .request()
        .get(Response.class);

    int status        = response.getStatus();
    MediaType mt      = response.getMediaType();
    String spacesJson = response.readEntity(String.class);

    //Assertions here.
}

这是服务尝试查找数据源时的异常:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at uk.co.propx.ofpro.db.DBConnector.getDataSource(DBConnector.java:105)
at uk.co.propx.ofpro.db.DBConnector.getDB(DBConnector.java:100)
at uk.co.propx.ofpro.model.dao.OfficeSpaceDAO.get(OfficeSpaceDAO.java:101)
at uk.co.propx.ofpro.model.OfficeSpace.get(OfficeSpace.java:335)
at uk.co.propx.ofpro.api.v1.OfficeSpaceService.getSkipLimit(OfficeSpaceService.java:99)
at uk.co.propx.ofpro.api.v1.OfficeSpaceService.get(OfficeSpaceService.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
at org.glassfish.jersey.jdkhttp.JdkHttpHandlerContainer.handle(JdkHttpHandlerContainer.java:161)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

最佳答案

有一种方法:

ResourceConfig rc = new ResourceConfig(OfficeSpaceService.class);
rc.property("contextConfig",new ClassPathXmlApplicationContext(
new String[]{"src/main/webapp/META-INF/context.xml"},
ContextLoader.getCurrentWebApplicationContext()));

关于tomcat - 我的 JUnit 测试如何找到我的 Tomcat 数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424794/

相关文章:

eclipse - 无法在 Eclipse Neon 上安装新软件

java - 在Tomcat中部署WAR文件,部署后出现问题

java - Jersey 2.x ExceptionMapper 父映射

java - 为什么在 Tomcat 下运行的应用程序只使用了一半的可用 CPU?

java - 使用 Java 以编程方式重写 URL

java - 从 Junit 中的 Jax-Rs Response 类获取 header 值、状态代码等

java - 如何编写服务器端 java 代码以使用 JAX-RS(Jersey) 以流形式返回任何类型的文件?

java - 注入(inject)上下文 HttpServletRequest 原因 "interface org.glassfish.hk2.api.ProxyCtl is not visible from class loader"( Jersey 2.21,weblogic 12.1.3)

java - JAX-RS 在运行时更改 @XmlRootElement.name

java - 在 ubuntu 中安装 java、tomcat、eclipse 和 maven