Handling Dependency Injection in Scala Test Cases With TestNG
- How in Guice do you override production dependencies with test ones when you want to execute a test case?
- How do you use TestNG with Scala?
Here’s a simple example showing how this works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
Starting at the top with the
@Guice annotation. This identifies the class that TestNG will
use to create the Guice module to provide the dependencies for the test. This class, in
the example ExampleTestModuleFactory, has
to extend the IModuleFactory interface and return the module to use from the
createModule(context: ITextContext, testClass: Class[_]) method. Instead of a moduleFactory The
@Guice annotation can also
be used by simply listing the guice modules
@Guice(module = Array(classOf[ProductionModuleOne], classOf[ProductionModuleTwo])) but this doesn’t allow for replacing production dependencies with test ones.
In ExampleTextModuleFactory the
with methods on the
classes are used to combine the production dependencies and then override some of them with test dependencies.
The bindings in the ExampleTestModule override those for the same key in the production modules.
This lets you replace just the dependencies you need with test ones without having to replicate the complete production configuration.
Backticks must be used around override and with because they are reserved words in Scala.
To use the dependencies you just need to add the
@Inject annotation to the test class to inject values for variables, as in the example.
Unfortunately you can’t inject parameters into test methods using Guice, you have to use TestNG’s facilities for that.