Hi we got a spring application running on tomcat application-server. I want to add integration tests with Springboot in JUnit. I now got the problem that the embedded tomcat server does not store our datasource and therefore it cannot be looked up... If the server configuration does not have a datasource defined we got a fallback "context.xml" in our resources that should be used. But the embedded tomcat is not reading this configuration and I just cannot figure out, how to do this. The following is the point of failure in my JUnit Test, since it cannot find this JNDI-Name in the embedded tomcat:
@Bean
public DataSource dataSource()
{
return new JndiDataSourceLookup().getDataSource("jdbc/myDB");
}
the fallback context.xml looks like this:
<Context>
<!-- HSQL -->
<Resource name="jdbc/myDB" auth="Container"
type="javax.sql.DataSource" driverClassName="org.hsqldb.jdbcDriver"
url="jdbc:hsqldb:mem:mymemdb;shutdown=true" username="SA" password=""
maxTotal="100" maxIdle="5" maxWaitMillis="10000"/>
<Manager pathname=""/>
</Context>
how can I push this file into the embedded tomcat on startup?
Spring Boot offer many ways to externalize your configuration...
You can define your datasource properties in your tomcat/conf/Catalina/<host>
context descriptors.
You could define your jndi name:
<Context>
<Parameter name="spring.datasource.jndi-name" value="/pathToYourDatasource" />
</Context>
Or define the datasource:
<Context>
<Parameter name="spring.datasource.url" value="your url" />
<Parameter name="spring.datasource.username" value="your username" />
<Parameter name="spring.datasource.password" value="your password" />
</Context>
Even define the path to an application.properties
and set here your configuration:
<Context>
<Parameter name="spring.config.location" value="/path/to/application.properties" />
</Context>
This way is not necessary set programmatically your hardcoded datasource and you can put another database configuration for testing in src/test/resources/application.properties
:
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
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