1717package org .apache .dubbo .common .extension ;
1818
1919import org .apache .dubbo .common .URL ;
20+ import org .apache .dubbo .common .beans .factory .ScopeBeanFactory ;
2021import org .apache .dubbo .common .convert .Converter ;
2122import org .apache .dubbo .common .convert .StringToBooleanConverter ;
2223import org .apache .dubbo .common .convert .StringToDoubleConverter ;
@@ -666,10 +667,16 @@ void testLoadDefaultActivateExtension2() {
666667
667668 @ Test
668669 void testInjectExtension () {
669- // register bean for test ScopeBeanExtensionInjector
670- DemoImpl demoBean = new DemoImpl ();
671- ApplicationModel .defaultModel ().getBeanFactory ().registerBean (demoBean );
672- // test default
670+ // Work with the shared factory, but make registration idempotent.
671+ ScopeBeanFactory beanFactory = ApplicationModel .defaultModel ().getBeanFactory ();
672+ if (beanFactory .getBeansOfType (Demo .class ).isEmpty ()) {
673+ beanFactory .registerBean ("demo-test-singleton" , new DemoImpl ());
674+ }
675+ // After the (potential) registration, fetch the canonical DemoImpl(s).
676+ List <DemoImpl > impls = beanFactory .getBeansOfType (DemoImpl .class );
677+ DemoImpl demoBean = impls .get (0 );
678+
679+ // Exercise injection path
673680 InjectExt injectExt = getExtensionLoader (InjectExt .class ).getExtension ("injection" );
674681 InjectExtImpl injectExtImpl = (InjectExtImpl ) injectExt ;
675682 Assertions .assertNotNull (injectExtImpl .getSimpleExt ());
@@ -691,6 +698,13 @@ void testMultiNames() {
691698
692699 @ Test
693700 void testGetOrDefaultExtension () {
701+ // Ensure there is exactly one DemoImpl available to satisfy injection paths.
702+ ScopeBeanFactory beanFactory = ApplicationModel .defaultModel ().getBeanFactory ();
703+ List <DemoImpl > demos = beanFactory .getBeansOfType (DemoImpl .class );
704+ if (demos == null || demos .isEmpty ()) {
705+ beanFactory .registerBean ("test-demo-bean" , new DemoImpl ());
706+ }
707+
694708 ExtensionLoader <InjectExt > loader = getExtensionLoader (InjectExt .class );
695709 InjectExt injectExt = loader .getOrDefaultExtension ("non-exists" );
696710 assertEquals (InjectExtImpl .class , injectExt .getClass ());
0 commit comments