Mohammad Najar
Mohammad Najar

Reputation: 2009

Spring Boot: How to Disable JNDI lookup and use spring.datasource instead for Testing?

I would like my main Spring Boot application to configure my datasource using JNDI via

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

in my application.properties file.

At time, I'd like to use a different datasource settings to be able to run my test cases using the in-memory datasource (i.e. H2). I've created a separate application.properties file under src/test/resources and have the following in it:

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

However, the test files don't seem to like this and I get the following error:

[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

causing my tests to fail.

Things I've done:

Renamed my properties file to testapplication.properties in src/test/resources and updating the content with

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

and the adding the following annotations to my test class:

@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
}

What am I missing here?

Upvotes: 4

Views: 6884

Answers (1)

Manisha
Manisha

Reputation: 111

I had exactly same situation as the question asked and the same error. This worked for me.

You can add the following to your src/test/resources/application.properties so that jndi lookup doesn't happen during tests -

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

Upvotes: 2

Related Questions