Skip to content

TestRestTemplateContextCustomizer and WebTestClientContextCustomizer can cause early FactoryBean instantiation #15898

Closed
@darkMechanicum

Description

@darkMechanicum

Hi!

Description

A assume there is a bug in spring-boot-test, related to TestRestTemplate BeanDefinition initialization.
To be precise, class TestRestTemplateContextCustomizer#TestRestTemplateRegistrar in it's method postProcessBeanDefinitionRegistry calls BeanFactoryUtils#beanNamesForTypeIncludingAncestors.
As beanNamesForTypeIncludingAncestors says, that's method invocation can cause FactoryBean initialization (and their dependencies, of course). And, in context of BeanDefinitionRegistryPostProcessor this, I guess, is incorrect behavior.

Reproducing

One file reproducer is as follows:

@Configuration
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SingleFileReproducerTest.class,
        webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@EnableAutoConfiguration
public class SingleFileReproducerTest {

    @Test
    public void dummy() {}

    @Component
    public static class SimpleFactoryBean implements FactoryBean {
        public SimpleFactoryBean(ApplicationContext context) {}
        public Object getObject() { return new Object(); }
        public Class<?> getObjectType() { return Object.class; }
    }
}

Note, when webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT is commented out, test passes, as there is no need for TestRestTemplate.

Versions

  1. org.springframework.boot.spring-boot-starter:2.1.2.RELEASE
  2. org.springframework.boot.spring-boot-starter-web:2.1.2.RELEASE
  3. org.springframework.boot.spring-boot-starter-test:2.1.2.RELEASE
  4. junit.junit:4.12
  5. Java 1.8

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions