Above is codes in main.kolitn.~~
package com.example.demo   // in main folder
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}
@Configuration("show")
class Show {
    init {
        System.err.println("[main] this is Test#init")
    }
    @Bean("map")
    fun map(): Map<Int, Int> {
        System.err.println("[main] testMap bean loaded...")
        return mapOf(10 to 11, 12 to 13)
    }
}
and this is codes in test.kotlin.~~~
package com.example.demo     // in test folder
@SpringBootTest
class DemoApplicationTests {
    @Autowired
    lateinit var map: Map<Int, Int>
    @Test
    fun contextLoads() {
        System.err.println("this is test")
        System.err.println("[test] map : $map")
    }
}
//@Configuration("test")
open class Show {
    init {
        System.err.println("[test] this is Test#init")
    }
    @Bean("map")
    fun map(): Map<Int, Int> {
        System.err.println("[test] map bean loaded...")
        return mapOf(1 to 2, 3 to 4)
    }
}
when I run the DeomoApplicaitonTests#contextLoads(), I expected Show class in main folder is scanned and [main] brabra logs will be printed in console because Show class in test folder. But actual output is
[test] this is Show#init
[test] map bean loaded...
this is test
[test] map : {1=2, 3=4}
I don't understand why Show class in test folder was scanned even that doesn't have @Configuration annotation. Is there any document to explain this? thanks.
                        
In Spring, the
@SpringBootTestannotation generally scans for beans in the main application package and its sub-packages. Since your test classDemoApplicationTestsis in a different package (test), it won't automatically scan theShowclass in the main folder.However, the
@SpringBootTestannotation also loads the entire application context, which means it scans for beans in your entire project. TheShowclass in the test folder is being picked up because it has the same name as theShowclass in the main folder, and Spring is probably merging or overriding the beans during context loading.To control which configuration class is loaded during the test, you can use the
@ContextConfigurationannotation on your test class and specify the configuration class you want to use explicitly.Here's an example:
This should ensure that only the
Showclass from the main folder is considered during the test, and you should see the expected logs.