Multiple bean definitions (Conflicts)
See original GitHub issueYour documentation says “latest bean wins” (See https://insert-koin.io/docs/1.0/reference/koin-dsl/?#multiple-definitions)
The source code throws an exception if there are multiple beans: see https://github.com/Ekito/koin/blob/0.9.1/koin-core/src/main/kotlin/org/koin/KoinContext.kt#L121
In case future readers face this,
My use case: I want to override/replace/shadow specific beans with mocks/spies in my unit/integration tests.
My solution was to
- Register all the normal application beans (eg. via startKoin)
- Create a
BeanDefinition
instance for each mock/spy/fake - Register them manually
koin.beanRegistry.declare( theBeanDef , Scope.root())
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
Top Results From Across the Web
How to resolve this conflicting of two beans with same name in ...
Use the @Primary annotation on the bean defenition on the configuration class @Bean(name = "beanOnA") @Primary public YourInterface ...
Read more >When two beans collide - Tratif
When you have two beans with the same name in different configuration classes but without qualifiers — in such a case the application...
Read more >Instantiating Multiple Beans of the Same Class with Spring ...
In this article, we've learned about instantiating multiple beans of the same class using Spring annotations using three different approaches.
Read more >5. The IoC container - Spring
It can be useful to have bean definitions span multiple XML files. ... It detects configuration problems, such as references to non-existent beans...
Read more >Failing to run tests because of Conflicting Bean Definition ...
it was heavily due to conflicting bean definitions , that is between xml definitions in pre-2.2 core version and Annotation definition in 2.2 ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you want to reuse a module and override some definitions for your test, just make a module containing your new definitions and load modules in order: your app + your test module
The last module will override needed definitions
A bean is overriden when: same name, type and scope
You can match multiple bean for a given type if you use
bind
oeprators thenOf course you are correct about
KoinTest
being an interface. Clearly I didn’t look intoKoinTest
at all.As you mentioned, the root cause appears to be a choice of eager-injection versus lazy-injection.
I still prefer “immutable-after-construction” & eager injection, so I’ll use the workaround above.
Thanks for the helpful information.