It's my first question ever on stackoverflow.com.
Lately I have been looking for a way to change how Spring Boot (2.x) names its beans, created via @Component (etc) annotation.
I have found the SpringApplicationBuilder class, which thankfully allows to inject custom bean name generator:
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class)
.beanNameGenerator(customBeanNameGenerator)
.run(args);
}
}
But the problem is, despite the fact that this solution works for production, this does not hovewer work in JUnit 4.0 unit tests, where JUnit Runner SpringRunner recreates Spring Context for testing purposes - in that case SpringApplicationBuilder is not used whatsoever, and as a result our application startup fails, becouse of autowiring multi-beans candidates problems.
@RunWith(SpringRunner.class)
@SpringBootTest(classes= {Application.class} )
public class SomeControllerTest {
...
}
Is there any elegant way to make that unit tests use SpringApplicationBuilder as well?
Regards.