Skip to content

ClassCastException in TestRestTemplateContextCustomizer #15843

@slinstaedt

Description

@slinstaedt

When trying to decorate the ApplicationContext used for tests, the ones using a TestRestTemplate are failing with a ClassCastException. A brief glimpse reveals, that TestRestTemplateContextCustomizer is doing a type check on the beanFactory before upcasting, but is later on casting the context itself, which happens to work with most spring-provided ApplicationContexts, but not for custom ones, as long as they are not doing the same decoration trick as the spring ones. Additionally one would normally also expect the checked reference to be casted, and not some other one.

java.lang.ClassCastException: xxx.TestEnvironment$Loader$Context cannot be cast to org.springframework.beans.factory.support.BeanDefinitionRegistry
	at org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer.registerTestRestTemplate(TestRestTemplateContextCustomizer.java:65) ~[spring-boot-test-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	at org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer.customizeContext(TestRestTemplateContextCustomizer.java:58) ~[spring-boot-test-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:320) ~[spring-boot-test-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:649) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:373) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-2.1.2.RELEASE.jar:2.1.2.RELEASE]
	... many more common frames omitted

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions