| This issue started at Spring Boot issue tracker, https://github.com/spring-projects/spring-boot/issues/18671 Running DemoApplicationTests with Spring Boot 2.1.9 {{ 2019-10-21 12:21:49.913 INFO 10600 — [ main] com.example.demo.DemoApplicationTests : Started DemoApplicationTests in 3.577 seconds (JVM running for 5.145) 2019-10-21 12:21:49.960 INFO 10600 — [ main] c.example.demo.db.TenantSchemaResolver : resolveTenantIdentifier: tenant1 2019-10-21 12:21:50.068 INFO 10600 — [ main] SchemaBasedMultiTenantConnectionProvider : getConnection. currentSchemaName=PUBLIC, newSchemaName=tenant1 2019-10-21 12:21:50.070 INFO 10600 — [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@46f699d5 testClass = DemoApplicationTests, testInstance = com.example.demo.DemoApplicationTests@475835b1, testMethod = $spock_feature_0_0@DemoApplicationTests, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@18518ccf testClass = DemoApplicationTests, locations = '{}', classes = ' {class com.example.demo.DemoConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6db9f5a4, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1ebd319f, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3571b748, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5f16132a, org.spockframework.spring.mock.SpockContextCustomizer@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]]; transaction manager [org.springframework.orm.hibernate5.HibernateTransactionManager@6a902015]; rollback [true] Hibernate: insert into Book (id, name) values (null, ?) 2019-10-21 12:21:50.323 INFO 10600 — [ main] SchemaBasedMultiTenantConnectionProvider : releaseConnection: tenant1 2019-10-21 12:21:50.323 INFO 10600 — [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@46f699d5 testClass = DemoApplicationTests, testInstance = com.example.demo.DemoApplicationTests@475835b1, testMethod = $spock_feature_0_0@DemoApplicationTests, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@18518ccf testClass = DemoApplicationTests, locations = '{}', classes = '{class com.example.demo.DemoConfiguration} ', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = ' {org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@6db9f5a4, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1ebd319f, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@3571b748, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5f16132a, org.spockframework.spring.mock.SpockContextCustomizer@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.spockframework.spring.SpringMockTestExecutionListener.MOCKED_BEANS_LIST' -> list[[empty]]]] }} Running same test with 2.2.0 {{ 2019-10-21 12:22:31.047 INFO 10484 — [ main] com.example.demo.DemoApplicationTests : Started DemoApplicationTests in 3.533 seconds (JVM running for 5.141) 2019-10-21 12:22:31.176 INFO 10484 — [ main] SchemaBasedMultiTenantConnectionProvider : getConnection. currentSchemaName=PUBLIC, newSchemaName=PUBLIC 2019-10-21 12:22:31.177 INFO 10484 — [ main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@66629f63 testClass = DemoApplicationTests, testInstance = com.example.demo.DemoApplicationTests@644c78d4, testMethod = $spock_feature_0_0@DemoApplicationTests, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@841e575 testClass = DemoApplicationTests, locations = '{}', classes = '{class com.example.demo.DemoConfiguration}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true} ', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@a3d8174, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@272ed83b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2df9b86, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@76c3e77a, org.spockframework.spring.mock.SpockContextCustomizer@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]]; transaction manager [org.springframework.orm.hibernate5.HibernateTransactionManager@212e39ee]; rollback [true] Hibernate: insert into Book (id, name) values (null, ?) 2019-10-21 12:22:31.415 WARN 10484 — [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 42102, SQLState: 42S02 2019-10-21 12:22:31.415 ERROR 10484 — [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : Table "BOOK" not found; SQL statement: insert into Book (id, name) values (null, ?) [42102-199] 2019-10-21 12:22:31.445 INFO 10484 — [ main] SchemaBasedMultiTenantConnectionProvider : releaseConnection: PUBLIC 2019-10-21 12:22:31.445 INFO 10484 — [ main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test: [DefaultTestContext@66629f63 testClass = DemoApplicationTests, testInstance = com.example.demo.DemoApplicationTests@644c78d4, testMethod = $spock_feature_0_0@DemoApplicationTests, testException = org.hibernate.exception.SQLGrammarException: could not prepare statement, mergedContextConfiguration = [MergedContextConfiguration@841e575 testClass = DemoApplicationTests, locations = '{}', classes = ' {class com.example.demo.DemoConfiguration} ', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = ' {org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true} ', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@a3d8174, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@272ed83b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2df9b86, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@76c3e77a, org.spockframework.spring.mock.SpockContextCustomizer@0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.spockframework.spring.SpringMockTestExecutionListener.MOCKED_BEANS_LIST' -> list[[empty]]]] }} The difference is that the TenantSchemaResolver is not called before the transaction initializes in 2.2.0. The reason mentions on github issue is this improvement made in 5.4.5 , https://hibernate.atlassian.net/browse/HHH-13565 What I would like to have is a transaction that is rolled back, which I have in 5.4.4, but being able to reach a tenant schema. |