我的JUnit Test如何找到我的Tomcat DataSource?

时间:2017-07-31 20:32:13

标签: 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.
}

当服务尝试查找DataSource时,这是异常:

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)

1 个答案:

答案 0 :(得分:0)

有一种方法:

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