android - 尝试使用 Mockito 在 Presenter 中测试 void 方法时出现 NullPointerException
问题描述
我正在尝试使用 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()
在我的 Presenter 中测试该方法?这是我到目前为止所尝试的:
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)
解决方案
多处出错。
第一的:
@Mock
private CitiesContract.Model citiesModel;
这基本上可以通过以下方式撤消:
citiesModel = new CitiesModel();
仅注释就足以创建一个模拟对象(准确地说:注释与该initMocks()
调用一起)。
但是您随后进入并创建了一个真实的对象,然后您尝试为其添加规范。但是当这里被调用时:
when(citiesModel.getCityList()).thenReturn(null);
如前所述,citiesModel
不是 Mockito 创建的模拟,而是您的生产类的真实对象。
那是行不通的。因此,首先citiesModel = new CitiesModel();
从您的代码中删除。
下一个:
你model = new CitiesModel();
在你的演讲课上做。但是没有魔法可以以某种方式将您的模拟模型实例放入演示者类的该字段中。
换句话说:仅仅声明一个模拟是不够的。你必须确保它被注入到你的测试类中。通过使用“仅测试”构造函数(需要更多参数)或使用 @InjectMocks 注释来传递它。
推荐阅读
- java - 如何使用来自控制台的输入
- python - 如何将重复的数据行排列成一行
- android - 如何在同一活动android中将变量从一种方法传递给另一种方法
- c++ - 为什么字符串比较会导致无限循环
- angularjs - Angular 1.6.1 ng--option 值以 type 为前缀:
- swift - PromiseKit 6、Alamofire、Xcode 10.2/Swift 5
- javascript - 如何在javascript中舍入十进制数
- maya - Mel Button Color ScriptJob?
- knockout.js - 如何使用 KnockoutJs 更改我的语言内容?
- sql - 根据条件获取列的总和