gpt4 book ai didi

jakarta-ee - 使用 spring 在 junit 中查找 Jndi

转载 作者:行者123 更新时间:2023-12-04 05:54:22 25 4
gpt4 key购买 nike

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

最佳答案

这里引用了 Spring 官方文档 http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/testing.html#mock-objects-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.



在这个文档中,我使用了声明数据源的 Spring 上下文配置类:
@Configuration
public class JndiDataConfig {
@Bean
public DataSource dataSource() throws Exception {
Context ctx = new InitialContext();
return (DataSource) ctx.lookup("java:comp/env/jdbc/datasource");
}
}

关于jakarta-ee - 使用 spring 在 junit 中查找 Jndi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9696195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com