我正在尝试使用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);
我如何成功通过此测试?预先感谢。
编辑:
根据要求,这是我的日志:
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)
答案 0 :(得分:1)
多件事错了。
第一:
@Mock
private CitiesContract.Model citiesModel;
基本上可以通过以下方式撤消:
citiesModel = new CitiesModel();
仅注释就足够足够来创建模拟对象(准确地说:注释连同该initMocks()
调用)。
但是随后您需要创建一个 real 对象,然后尝试为其添加规格。但是当在这里调用时:
when(citiesModel.getCityList()).thenReturn(null);
如前所述,citiesModel
不是Mockito创建的模拟,而是生产类的 real 对象。
那行不通。因此,首先从代码中删除citiesModel = new CitiesModel();
。
下一步:
您在主持人班上做model = new CitiesModel();
。但是,没有任何魔术可以将您的模拟模型实例放入presenter类的该字段中。
换句话说:仅声明一个模拟是不够的。您必须确保将它注入到正在测试的班级中。通过使用“仅测试”构造函数(需要更多参数)传递它,或使用@InjectMocks批注。