Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittesting IoC registration?

Should you unittest the code that registers components into your IoC container?

If so, how?

like image 766
Andreas Avatar asked Jan 12 '09 15:01

Andreas


3 Answers

In spring you can have a unit test that simply loads the application-context without asserting anything. It's actually a fairly useful test in conjunction with automatic build, since spring complains about a lot of problems when loading the full context.

like image 81
krosenvold Avatar answered Sep 22 '22 11:09

krosenvold


It somehow feels wrong to me to have the IoC container running in my test-projects. I also noticed that most of the bugs that are caused by dependencies not being resolved are caused by the order dependecies are resolved, this is very hard to test right and is not something I'd want to do as a unit test.

Usually I use Debug.Assert statements in the initialization routines of my classes. This gives me an early warning system for IoC related errors and also helps specify dependencies better in my code.

like image 44
Mendelt Avatar answered Sep 18 '22 11:09

Mendelt


What I do with the Guice IoC container, is that first I produce the classes for some feature using TDD without Guice (these are unit tests). Then I create an integration test for that feature with Guice. At that point the IoC configuration (Guice module) is incomplete, so the integration test will fail. Using TDD, I add the IoC configuration step by step until the integration test passes. I won't add any @Inject annotation, line of configuration or scope declaration, unless it is required to pass a test. As a result, I will have integration (or acceptance) tests making sure that the IoC configuration is right and complete.

This same method should work with any IoC container or other system, whose configuration is so complex that it may break - don't write any configuration unless it is require to pass a test.

like image 38
Esko Luontola Avatar answered Sep 20 '22 11:09

Esko Luontola