Guice injector in JUnit tests

Alexis Dufrenoy picture Alexis Dufrenoy · Apr 12, 2011 · Viewed 53.8k times · Source

Using Guice, is it a good practice to get a new injector in each JUnit test class, as each test class should be independant?

Answer

Michael Lloyd Lee mlk picture Michael Lloyd Lee mlk · Apr 12, 2011

You should really avoid using Guice in unit tests as each test should be small enough that manual DI is manageable. By using Guice (or any DI) in unit tests you are hiding away a warning that your class is getting too big and taking on too many responsibilities.

For testing the bootstrapper code and integration tests then yes create a different injector for each test.