Skip to content

Nested test classes don't inherit properties from slice test annotations on enclosing class #33317

Closed
@vpavic

Description

@vpavic

Assuming a test class utilizing a Spring Boot slice test annotation along these lines:

@JdbcTest(properties = "spring.datasource.url=jdbc:tc:postgresql:14.6-alpine:///test")
@AutoConfigureTestDatabase(replace = Replace.NONE)
class Tests {

	@Test
	void test() {
	}

	@Nested
	class NestedTests {

		@Test
		void nestedTest() {
		}

	}

}

Test named test will succeed, while the nestedTest will fail with root cause of:

Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
	at app//org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:182)
	at app//org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:123)
	at app//org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48)
	at app//org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90)
	at java.base@17.0.5/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base@17.0.5/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base@17.0.5/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base@17.0.5/java.lang.reflect.Method.invoke(Method.java:568)
	at app//org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
	... 118 more

This is because JdbcTestContextBootstrapper looks up properties using SearchStrategy.INHERITED_ANNOTATIONS strategy and therefore does not pick up ones supplied on the enclosing class's annotation. The same behavior is present in all subclasses of SpringBootTestContextBootstrapper.

The observed behavior was surprising to me, because based on Framework's support for nested test classes where test configuration is inherited from the enclosing class, I expected Spring Boot's slice tests to be aligned with that.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions