Currently I try to understand how the @Injectable
and @Tested
annotations are working. I already did some tests and understood the concept but I didn\'
Good question. The thing to notice about JMockit's (or any other mocking API) support for dependency injection is that it's meant to be used only when the code under test actually relies on the injection of its dependencies.
The example Translator
class does not rely on injection for the TranslatorWebService
dependency; instead, it obtains it directly through internal instantiation.
So, in a situation like this you can simply mock the dependency:
public class TranslatorTest {
@Tested Translator tested;
@Mocked TranslatorWebService transWebServiceDependency;
@Test public void translateEnglishToGerman() {
new Expectations() {{
transWebServiceDependency.performTranslation("House");
result = "Haus";
}};
String translated = tested.translateEnglishToGerman("House");
assertEquals("Haus", translated);
}
}