fix: resolve @InjectMockKs initialization order based on dependencies#1500
fix: resolve @InjectMockKs initialization order based on dependencies#1500Raibaz merged 4 commits intomockk:masterfrom
Conversation
|
Thanks a lot for putting this together! I'm a bit worried about the performances, though. Would it make sense to make the topological sorting of dependencies opt-in and not enabled by default, for instance by adding a flag to Do you have any data about the impact of topological sorting on the performances? |
|
I also wanted to ask you if the flag method would be good, but you just pointed it out! |
|
I modified the dependency sorting to be applied via a flag. For now, I'll add the changes and performance test results. Changes
How to enable the flag
Performance impact of DependencyOrder in Mock initializationI ran JMH benchmarks comparing initWithDependencyOrder vs initWithoutDependencyOrder across different dependency graph shapes (independent, wide, linear, diamond) and sizes. Focusing on the more stable and realistic case (size = 20, thrpt mode): Overall, applying DependencyOrder consistently introduces a 3~5% throughput regression compared to the unordered version. Therefore, applying the flag seems appropriate. main summary
|
|
Thanks a lot for looking into the performances of this change! 🙏 When we release it, let's make sure to document that switching on the |
|
@neungs-2 this looks good to me. Can you please add documentation for it in README.md so that we can merge it? Thanks! |
|
@Raibaz Thank you for the quick review! I've added the |
Summary
@InjectMockKsfailing when dependency order differs from field declaration order@InjectMockKsProblem
When multiple
@InjectMockKsfields have dependencies on each other, MockK processed them in reflection order (roughly alphabetical), causing "No matching constructors found" errors.Solution
Analyze constructor parameter types to build a dependency graph, then use topological sort to determine correct initialization order.
Changes
modules/mockk/src/jvmMain/kotlin/io/mockk/impl/annotations/JvmMockInitializer.ktmodules/mockk/src/jvmMain/kotlin/io/mockk/impl/annotations/InjectionHelpers.ktmodules/mockk/src/commonTest/kotlin/io/mockk/it/InjectMocksTest.ktTest
Closes #1496