Although my previous issue was closed I still insist it is an issue. Maybe my post was not clear or @wilkinsona haven't read it carefully. Because I can't reopen the issue I've decided to create a new one.

Branch with bug reproduce is https://github.com/gredwhite/springboottestconfigissue_demo/tree/bug/config_issue

Let me shortly explain. I've created test example where I want to replace production bean in tests. Production configuration requires special properties in application.yaml(for demo purpose)

package com.example.demo.app
...

@Configuration
@EnableConfigurationProperties(MyProperties::class)
open class MyConfiguration(
    private val myProperties: MyProperties
) {
    @Bean
    open fun propertiesHolder(): PropertiesHolder {
        return PropertiesHolder(arrayOf(myProperties.prop1, myProperties.prop2, myProperties.prop3))
    }
}

In tests:

package com.example.demo.app
...

@TestConfiguration
open class MyConfigurationTest {  // <-- different name
    @Bean
    open fun propertiesHolder(): PropertiesHolder = mockk()
}

And test looks like this:

@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@Import(MyConfigurationTest::class)
class DemoControllerTest {
    @Autowired
    lateinit var testRestTemplate: TestRestTemplate

    @Test
    fun getItems_success() {

    }
}

Also in tests application.yml I have:

spring:
  main:
    allow-bean-definition-overriding: true

When I start the test I see:

Actual result:


APPLICATION FAILED TO START


Description:

Failed to bind properties under 'my.props' to com.example.demo.app.MyProperties:

Reason: java.lang.NullPointerException: Parameter specified as non-null is null: method com.example.demo.app.MyProperties.<init>, parameter prop1

Action:

Update your application's configuration

So configuration declared in test doesn't overrride production configuration

Expected result:

Test starts application context without production bean propertiesHolder and uses mock declared in test configuration class inside MyConfigurationTest

SO post: https://stackoverflow.com/questions/77202781/testconfiguration-annotation-doesnt-override-bean-from-production-configuratio

Comment From: philwebb

Although my https://github.com/spring-projects/spring-boot/issues/37634 was closed I still insist it is an issue.

I don't think it is. Please see my answer at https://stackoverflow.com/a/77205870/1526192

Comment From: gredwhite

Thank you. But I still disagree with you because I have no option how I can override the bean in test configuration without 100% hiding it.

Comment From: gredwhite

@philwebb could you please take a look?

Comment From: philwebb

@gredwhite This conversation doesn't belong in this issue tracker, let's keep it on stackoverflow.com please.

Comment From: gredwhite

@philwebb I insist that it is a bug but you disagree. Why can't we discuss it here ?

Comment From: philwebb

@gredwhite We prefer to keep the issue tracker for Spring Boot bugs and enhancements. We don't consider this a bug as it's working as designed, but if you did want changes to the way that @Configuration classes are processed you'd need to raise that with the Spring Framework team. Their issue tracker is at https://github.com/spring-projects/spring-framework/issues/.

Comment From: gredwhite

@philwebb but please give a response for the last question. I want to repeat the case described in that topic - https://stackoverflow.com/questions/50607285/spring-boot-testconfiguration-not-overriding-bean-during-integration-test

Please read question and accepted answer. Have I missed something? From my point of view I did all to repeat it and it follows the design(or not?). But I want to know if I missed something. Maybe it was working in spring boot 2 ?

Please, help.