NullPoInterException尝试使用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;
    }
}

如何在主持人中测试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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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;

基本上是 undone

citiesModel = new CitiesModel();

单独的注释是足够的创建模拟对象(确切:确切:与该initMocks()调用的注释一起(。

但是您然后进来并创建 real 对象,然后尝试为其添加一个规范。但是当这里调用此问题时:

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

正如所说,citiesModel不是摩根托创建的模拟,而是您生产类的A 真实对象。

无法正常工作。因此,首先从您的代码中删除citiesModel = new CitiesModel();

下一步:

您在主持人类中进行model = new CitiesModel();。但是,没有魔法会以某种方式将您的模型实例放入演示者类的领域。

换句话说:仅声明一个模拟就不够。您必须确保将其注入进入您的课程中。通过使用"仅测试"构造函数(需要更多参数(或使用@InjectMocks注释来传递它。

相关内容

  • 没有找到相关文章

最新更新