Im écrire des tests unitaires pour le code ci-dessous, Im obtenir NPE même si Im mocking les champs, Comment puis-je résoudre cela. Ces champs sont présents avec @Inject annotationComment sortir avec NPE pour le champ @Inject pendant le test unitaire?
@Component
interface A {
void run();
}
class B {
@Inject
A a;
void someMethod() {
a.run();
}}
class C{
@Inject
B b;
void anotherMethod() {
b.someMethod();
}
}
class CTest {
@Mock
B b;
// remains null when invoked in the actual class though its mocked instance is
// present here
@Mock
A a;
//// remains null when invoked in the actual class though its mocked instance
//// is present here
@InjectMocks
C c;
@Before
public void initialize() {
MockitoAnnotations.initMocks(this);
}
@Test
public void test() {
c.anotherMethod();
}
}
Alors, comment puis-je obtenir la valeur moquée dans la classe réelle où le champ est injecté à l'aide @Inject Mockito?
Hey merci pour la réponse, Ce code est juste par exemple et pas mon code réel. Il est suggéré d'utiliser soit @RunWith (MockitoJUnitRunner.class) ou MockitoAnnotations.initMocks (this) - se référer http://stackoverflow.com/a/28969255/2340345 – user2340345