Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

java - Spring Boot with datasource when testing

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...