Jndi使用spring在junit中查找

编程入门 行业动态 更新时间:2024-10-14 08:25:21
本文介绍了Jndi使用spring在junit中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在junit测试类中查找Jndi名称?我不想为junit使用单独的configration文件而不提供数据源属性?我特别需要在我的测试类中进行jndi查找。

How can I lookup Jndi name in junit test class? I do not want to use separate configration file for junit and not giving datasource properties? I specifically need to do jndi lookup in my test class.

推荐答案

以下是来自官方Spring文档的引用 http://static.springsource / spring / docs / 3.1.x / spring-framework-reference / html / testing.html#mock-objects-jndi :

Here is a quote from the official Spring docs static.springsource/spring/docs/3.1.x/spring-framework-reference/html/testing.html#mock-objects-jndi:

org.springframework.mock.jndi包中包含JNDI SPI的实现,您可以使用它为测试套件或独立应用程序设置简单的JNDI环境。

The org.springframework.mock.jndi package contains an implementation of the JNDI SPI, which you can use to set up a simple JNDI environment for test suites or stand-alone applications.

从这个doc我使用Spring上下文配置类来声明一个数据源:

From this doc I use Spring context configuration class which declares a datasource:

@Configuration public class JndiDataConfig { @Bean public DataSource dataSource() throws Exception { Context ctx = new InitialContext(); return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource"); } }

更多推荐

Jndi使用spring在junit中查找

本文发布于:2023-10-26 18:28:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Jndi   spring   junit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!