Registering test bean with same name in Spring Framework 5.1












1















I'm having following config in my production files:



@Configuration
internal class Config {

@Bean
fun clock() = Clock.systemUTC()
}


In tests:



@Configuration
class ClockTestConfiguration {

@Bean
fun clock() = SetableClock()
}


My test annotations:



@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...


When I was using Spring Boot 2.0.5.RELEASE it worked like a charm. After upgrading to 2.1.0.RELEASE it fails during bean registration.



Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration: 
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)


Is there a clean way to override such bean?










share|improve this question




















  • 1





    you can separate your configurations using profiles

    – stacker
    Nov 20 '18 at 21:07











  • yes, but I wonder if there is a cleaner option...

    – pixel
    Nov 20 '18 at 21:15
















1















I'm having following config in my production files:



@Configuration
internal class Config {

@Bean
fun clock() = Clock.systemUTC()
}


In tests:



@Configuration
class ClockTestConfiguration {

@Bean
fun clock() = SetableClock()
}


My test annotations:



@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...


When I was using Spring Boot 2.0.5.RELEASE it worked like a charm. After upgrading to 2.1.0.RELEASE it fails during bean registration.



Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration: 
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)


Is there a clean way to override such bean?










share|improve this question




















  • 1





    you can separate your configurations using profiles

    – stacker
    Nov 20 '18 at 21:07











  • yes, but I wonder if there is a cleaner option...

    – pixel
    Nov 20 '18 at 21:15














1












1








1








I'm having following config in my production files:



@Configuration
internal class Config {

@Bean
fun clock() = Clock.systemUTC()
}


In tests:



@Configuration
class ClockTestConfiguration {

@Bean
fun clock() = SetableClock()
}


My test annotations:



@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...


When I was using Spring Boot 2.0.5.RELEASE it worked like a charm. After upgrading to 2.1.0.RELEASE it fails during bean registration.



Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration: 
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)


Is there a clean way to override such bean?










share|improve this question
















I'm having following config in my production files:



@Configuration
internal class Config {

@Bean
fun clock() = Clock.systemUTC()
}


In tests:



@Configuration
class ClockTestConfiguration {

@Bean
fun clock() = SetableClock()
}


My test annotations:



@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...


When I was using Spring Boot 2.0.5.RELEASE it worked like a charm. After upgrading to 2.1.0.RELEASE it fails during bean registration.



Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration: 
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)


Is there a clean way to override such bean?







spring spring-boot kotlin spring-framework-beans






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 22:17







pixel

















asked Nov 20 '18 at 21:00









pixelpixel

9,8732190143




9,8732190143








  • 1





    you can separate your configurations using profiles

    – stacker
    Nov 20 '18 at 21:07











  • yes, but I wonder if there is a cleaner option...

    – pixel
    Nov 20 '18 at 21:15














  • 1





    you can separate your configurations using profiles

    – stacker
    Nov 20 '18 at 21:07











  • yes, but I wonder if there is a cleaner option...

    – pixel
    Nov 20 '18 at 21:15








1




1





you can separate your configurations using profiles

– stacker
Nov 20 '18 at 21:07





you can separate your configurations using profiles

– stacker
Nov 20 '18 at 21:07













yes, but I wonder if there is a cleaner option...

– pixel
Nov 20 '18 at 21:15





yes, but I wonder if there is a cleaner option...

– pixel
Nov 20 '18 at 21:15












1 Answer
1






active

oldest

votes


















6














You could use the properties attribute of @SpringBootTest to set spring.main.allow-bean-definition-overriding=true.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401442%2fregistering-test-bean-with-same-name-in-spring-framework-5-1%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You could use the properties attribute of @SpringBootTest to set spring.main.allow-bean-definition-overriding=true.






    share|improve this answer




























      6














      You could use the properties attribute of @SpringBootTest to set spring.main.allow-bean-definition-overriding=true.






      share|improve this answer


























        6












        6








        6







        You could use the properties attribute of @SpringBootTest to set spring.main.allow-bean-definition-overriding=true.






        share|improve this answer













        You could use the properties attribute of @SpringBootTest to set spring.main.allow-bean-definition-overriding=true.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 23:29









        Andy WilkinsonAndy Wilkinson

        59k9139148




        59k9139148
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401442%2fregistering-test-bean-with-same-name-in-spring-framework-5-1%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini