Как мой тест JUnit может найти мой источник данных Tomcat?

Я пытаюсь проверить мои REST Services. Я использую JerseyTest Jersey2 и предоставленную инфраструктуру 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)

1 ответ

Есть один из способов:

ResourceConfig rc = new ResourceConfig(OfficeSpaceService.class);
rc.property("contextConfig",new ClassPathXmlApplicationContext(
new String[]{"src/main/webapp/META-INF/context.xml"},
ContextLoader.getCurrentWebApplicationContext()));
Другие вопросы по тегам