在我的模拟依赖中获取NullPointerException

时间:2016-06-16 21:32:59

标签: java junit mockito

我有这个测试:

@RunWith(MockitoJUnitRunner.class)
public class MainClassTest {

@Mock
Dependence dependence;

@InjectMocks
MainClass mainClassTester;

}

这个测试:

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

    }

我的MainClass类看起来像:

public class MainClass{

 private Dependence dependence = new Dependence() ;

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

我在这一行得到了NullPointer:

boolean status = dependence.getStatus();

为什么没有模拟"依赖"工作?当我使用@inject时,此代码始终有效,但不能在此使用。

1 个答案:

答案 0 :(得分:-1)

如果要使用构造函数来创建对象而不是@Inject,则需要模拟构造函数而不是仅使用@Mock。

@InjectMock只会注入@Inject使用的字段。如果您有任何由新构造函数设置的字段,如果您使用@InjectMock,则不会在测试对象中注入。 来自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.