Reputation: 73
So i'm having problems trying to make my test loading the .sql script for schema and data.
By default my scripts are not being loaded, cause when my test try to get the data from the H2 db, show the message "Table "" not found".
The scripts are in the "src/test/resources" folder. H2 dependency in pom:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
And when i put the annotation "@Sql(scripts = { "/schema.sql", "/data.sql" }), i see the next error:
java.lang.IllegalStateException: Failed to execute SQL scripts for test context [DefaultTestContext@7090eaec testClass = MicroserviceControllerTest, testInstance = sche.invm.backend.rke.asn2019.get.controller.MicroserviceControllerTest@180274b1, testMethod = testGetPerson@MicroserviceControllerTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@5158e446 testClass = MicroserviceControllerTest, locations = '{}', classes = '{class sche.invm.backend.rke.asn2019.get.GetApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@30bbcf91, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@52b959df, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@31834a2b, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@34c53688, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@6127a7e], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]]: could not obtain DataSource from transaction manager [org.springframework.orm.jpa.JpaTransactionManager] (named '').
This is the configuration of my test:
This is the configuration of my test with the @Sql annotation:
¿What it can be the error? My app is configured with multitenant, i don't know if something of this can be causing this particular error.
Upvotes: 4
Views: 3452
Reputation: 73
In the end the problem was that in the Application of my service the property "exclude = {DataSourceAutoConfiguration.class}" was being declared inside @SpringBootApplication(), after removing it my test ran without problem.
Another point was that because my service is multitenant, then I had to configure in my properties file, apart from the url of my multitenant, also the url of spring.datasource (this because my multitenant url was the following jdbc: h2: mem: testdb, but the db h2 was setting the url this way jdbc: h2: mem: 718e98df-3d08-46de-99e3-6ec4dc725293, so the scripts were being executed in this db and the other db didn't have any of these tables).
I hope it's understood (my English is not the best)
This is part of my test properties file:
spring.datasource.url:jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username= sa
spring.datasource.password=
#multitenant datasources
datasources.0.tenant=CL
datasources.0.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
datasources.0.username=sa
datasources.0.password=
Upvotes: 1