2016-06-16 6 views
-1

bekommen habe ich diesen Test:Nullpointer in meiner Mock Abhängigkeit

@RunWith(MockitoJUnitRunner.class) 
public class MainClassTest { 

@Mock 
Dependence dependence; 

@InjectMocks 
MainClass mainClassTester; 

} 

Und diesen Test:

@Test 
    public void testA() { 
    when(dependence.getStatus()).thenReturn(true); 
    mainClassTester.startStatusOperation(); 

    } 

Meine Mainclass-Klasse wie folgt aussieht:

public class MainClass{ 

private Dependence dependence = new Dependence() ; 

public boolean startStatusOperation(){ 
    boolean status = dependence.getStatus(); 
    [...] 
} 
} 

Im bekommen Nullpointer in diesem Linie:

boolean status = dependence.getStatus(); 

Warum funktioniert die Scheinabhängigkeit nicht? Dieser Code hat immer funktioniert, wenn ich @inject verwendet habe, aber in diesem nicht verwenden kann.

+0

Haben Sie Konstruktor für Ihre Mainclass haben? – Vijay

+1

Wenn Sie Hilfe benötigen, müssen Sie einen COMPLETE-Stack-Trace veröffentlichen. –

Antwort

-1

Wenn Sie Konstruktor verwenden möchten, um ein Objekt anstelle von @Inject zu erstellen, müssen Sie den Konstruktor vortäuschen, anstatt nur @Mock zu verwenden.

@InjectMock injiziert nur das Feld, das Sie mit @Inject verwenden. Wenn Sie ein Feld haben, das von einem neuen Konstruktor gesetzt wird, wird dies nicht in das Testobjekt eingefügt, wenn Sie @InjectMock verwenden. Von http://site.mockito.org/mockito/docs/current/org/mockito/InjectMocks.html

@Documented 
@Target(value=FIELD) 
@Retention(value=RUNTIME) 
public @interface InjectMocks 
Mark a field on which injection should be performed. 
Allows shorthand mock and spy injection. 
Minimizes repetitive mock and spy injection. 
Mockito will try to inject mocks only either by constructor injection, setter injection, or property injection in order and as described below. If any of the following strategy fail, then Mockito won't report failure; i.e. you will have to provide dependencies yourself. 

Constructor injection; the biggest constructor is chosen, then arguments are resolved with mocks declared in the test only. If the object is successfully created with the constructor, then Mockito won't try the other strategies. Mockito has decided to no corrupt an object if it has a parametered constructor. 
Note: If arguments can not be found, then null is passed. If non-mockable types are wanted, then constructor injection won't happen. In these cases, you will have to satisfy dependencies yourself. 

Property setter injection; mocks will first be resolved by type (if a single type match injection will happen regardless of the name), then, if there is several property of the same type, by the match of the property name and the mock name. 
Note 1: If you have properties with the same type (or same erasure), it's better to name all @Mock annotated fields with the matching properties, otherwise Mockito might get confused and injection won't happen. 

Note 2: If @InjectMocks instance wasn't initialized before and have a no-arg constructor, then it will be initialized with this constructor. 

Field injection; mocks will first be resolved by type (if a single type match injection will happen regardless of the name), then, if there is several property of the same type, by the match of the field name and the mock name. 
Note 1: If you have fields with the same type (or same erasure), it's better to name all @Mock annotated fields with the matching fields, otherwise Mockito might get confused and injection won't happen. 

Note 2: If @InjectMocks instance wasn't initialized before and have a no-arg constructor, then it will be initialized with this constructor. 
+0

Das ist es, ich kann injectMocks ohne @injects nicht verwenden, ich brauche einen Konstruktor, der mein Attribut oder eine Setter-Methode setzt. danke für die Hilfe. – deveduardo