Skip to content
Sharp

Dependency Injection · interview question

Why might you prefer constructor injection over field injection when unit testing?

Multiple choiceMediumchapter-9-curatedpremiumdependency-injectionconstructor-vs-field-injection
Choose one answer
Answer and explanation

Correct answer

D. Constructor injection allows easier mocking of dependencies.

Why

Constructor injection allows you to easily pass mock objects as dependencies during testing, without needing to rely on reflection or other means to inject dependencies into private fields.

Test yourself on Dependency Injection

A free test with instant, explained results. No account needed.

Start free test
All Dependency Injection questions →