I am using Spring Boot application and the auto cofiguration is enabled. The main Application file is marked as @EnableAutoConfiguration
. The datasource is lookedup from JNDI is configured using java config and the class which create the datasource is marked as @Configuration
.
I have a test class as below.
@RunWith( SpringJUnit4ClassRunner.class )
@WebAppConfiguration
@ContextConfiguration( classes = Application.class )
public class TestSomeBusiness {}
The issue is when I run the test case, the datasource jndi lookup happens, which fails because the test case is not running inside a server environment. As far as I know the classes in classpath marked with @Configuration
are executed and that the reason the datasource lookup is being called.
The work around for now I have found is instead of JNDI lookup create the datasource using DriverManagerDataSource
, so that even if its not a server environment the datasource lookup won't fail.
My questions are:
1) How do we generally deal with datasource (when looking up from JNDI) in spring boot application for testing ?
2) Is there a way to exclude the datasource configuration class from being called when executing test case ?
3) Should I create an embedded server so that the JNDI lookup can be done when executing test case ?
Spring boot datasource configuration is nothing but the factory of connection which was used in a physical data source. Spring boot datasource uses the database credential to set up connections between the database server, it is alternative to the facility of Driver Manager.
To configure your own DataSource , define a @Bean of that type in your configuration. Spring Boot reuses your DataSource anywhere one is required, including database initialization. If you need to externalize some settings, you can bind your DataSource to the environment (see “Section 25.8.
@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests.
2) Is there a way to exclude the datasource configuration class from being called when executing test case ?
You can add a application.properties
config file into your src/test/resources
and spring boot would pick those configurations in test environments. I suppose, you have application.properties
in your src/main/resources
like this:
spring.datasource.jndi-name=some_jndi
This JNDI
resource will be used in your production environment. For your test environment you can use a, say MySQL database, by adding these configurations into your test application.properties
:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
3) Should I create an embedded server so that the JNDI lookup can be done when executing test case ?
As i said, you can totally bypass the fact that you're using JNDI
for production by adding test specific configurations.
1) How do we generally deal with datasource (when looking up from JNDI) in spring boot application for testing ?
You can mock JNDI
resources using facilities available in org.springframework.mock.jndi
package. For example by using SimpleNamingContextBuilder
you can:
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("jndi_name", dataSource);
builder.activate();
The other option is, of course, using Non JNDI
resources in test environments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With