java - Spring Boot : How to Disable JNDI lookup and use spring. 数据源代替测试?

标签 java spring spring-boot datasource jndi

我希望我的主要 Spring Boot 应用程序使用 JNDI 配置我的数据源通过

spring.datasource.jndi-name=java:jboss/datasources/MyAppDS

在我的 application.properties文件。

有时,我想使用不同的数据源设置,以便能够使用内存数据源(即 H2 )运行我的测试用例。
我创建了一个单独的 application.properties文件下 src/test/resources并在其中包含以下内容:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

但是,测试文件似乎不喜欢这样,我收到以下错误:
[main] WARN  o.s.w.c.s.GenericWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JndiDataSourceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:jboss/datasources/MyAppDS'; nested exception is 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

导致我的测试失败。

我做过的事情:

将我的属性文件重命名为 testapplication.propertiessrc/test/resources并更新内容
spring.mydatasource.driver-class-name=org.h2.Driver
spring.mydatasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.mydatasource.username=sa
spring.mydatasource.password=sa

并将以下注释添加到我的测试类中:
@RunWith(SpringRunner.class)
@WebAppConfiguration("classpath:META-INF/web-resources")
@TestPropertySource(locations="classpath:testapplication.properties")
@ConfigurationProperties(prefix="spring.mydatasource")
public class BrandsSvcTests {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext wac;  

    @Before
    public void setup() throws Exception {
         this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

      // ... my test cases
}

我在这里缺少什么?

最佳答案

我遇到了与所问问题完全相同的情况和相同的错误。这对我有用。
您可以将以下内容添加到您的 src/test/resources/application.properties 以便在测试期间不会发生 jndi 查找 -

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration

关于java - Spring Boot : How to Disable JNDI lookup and use spring. 数据源代替测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52899927/

相关文章:

java - Spring Qualifier,应用程序启动时的服务实现

java - 如何使 JTable 上的左键单击操作适应 Java 中的右键单击?

Java XMLBeans 抛出 NoSuchMethod 错误

java - Spring更改cglib

java - 使用 Postman 的 json 响应中的错误字符串

java - InputStream 在 MessageBodyReader Jersey 中提供空数据

Java 8 Stream API - 选择分组后的最低键

java - 使用点符号的 Thymeleaf 3 map 访问

java - 如何在初始握手时从 Spring Websocket 检索 session ID 并在以后使用?

java - 使用 Jackson/Springboot 解析可选 <T> 时出错