尝试使用 Mockito 在 Presenter 中测试 void 方法时出现 NullPointerException

NullPointerException when trying to test a void method in Presenter using Mockito

我正在尝试使用 Mockito 测试我的应用程序。它是使用 MVP 模式构建的。这是我的合同:

public interface CitiesContract {
    interface View {
        void addCitiesToList(List<City> cityList);
    }

    interface Presenter {
        void passCityListToView();
    }

    interface Model {
        List<City> getCityList();
    }
}

这是我的观点:

public class CitiesActivity extends AppCompatActivity implements CitiesContract.View {
    private List<City> cityList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cities);

        CitiesPresenter presenter = new CitiesPresenter(this);
        presenter.passCityListToView();
    }

    @Override
    public void addCitiesToList(List<City> cities) {
        cityList.addAll(cities);
    }
}

这是我的主持人:

public class CitiesPresenter implements CitiesContract.Presenter {
    private CitiesContract.View view;
    private CitiesModel model;

    public CitiesPresenter(CitiesContract.View view) {
        this.view = view;
        model = new CitiesModel();
    }

    @Override
    public void passCityListToView() {
        List<City> cityList = model.getCityList();
        view.addCitiesToList(cityList);
    }
}

这是我的模特:

public class CitiesModel implements CitiesContract.Model {
    @Override
    public List<City> getCityList() {
        List<City> cityList = new ArrayList<>();
        //Add 30 cities to the list
        return cityList;
    }
}

如何在 Presenter 中测试 passCityListToView() 方法?这是我到目前为止尝试过的:

public class CitiesPresenterTest {
    private CitiesContract.Presenter citiesPresenter;
    @Mock
    private CitiesContract.View citiesView;
    @Mock
    private CitiesContract.Model citiesModel;

    public void setUp() {
        MockitoAnnotations.initMocks(this);
        citiesPresenter = new CitiesPresenter(citiesView);
        citiesModel = new CitiesModel();
    }

    @Test
    public void testCityListIfNull() {
        when(citiesModel.getCityList()).thenReturn(null);
        citiesPresenter.passCityListToView();
        verify(citiesView).addCitiesToList(null);
    }
}

但我得到 NullPointerException 指向这一行:

when(citiesModel.getCityList()).thenReturn(null);

我怎样才能顺利通过这个测试?提前致谢。

编辑:

应要求,这是我的 logcat:

java.lang.NullPointerException
    at com.example.CitiesPresenterTest.testCityListIfNull(CitiesPresenterTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=16=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

多处错误。

第一个:

@Mock
private CitiesContract.Model citiesModel;

这基本上是撤消的:

citiesModel = new CitiesModel();

注释本身足以创建一个模拟对象(准确地说:注释连同那个initMocks()调用)。

但是您随后进入并创建了一个 真实的 对象,然后您尝试为其添加规范。但是当这里被调用时:

when(citiesModel.getCityList()).thenReturn(null);

如前所述,citiesModel 不是 Mockito 创建的模拟,而是您的作品 class 的 真实 对象。

那不行。因此,首先从您的代码中删除 citiesModel = new CitiesModel();

下一个:

您在演示者中 model = new CitiesModel(); class。但是没有魔法可以以某种方式将您的 mocked 模型实例放入演示者 class 的那个字段中。

换句话说:仅仅声明一个 mock 是不够的。您必须确保它被 注入 到您的 class 测试中。通过使用 "test only" 构造函数(需要更多参数)或使用 @InjectMocks 注释传递它。