我正在编写我的控制器类的测试用例,它有getModel()方法返回模型对象。以下是方法的片段:
@Override
public ContentModel getModel() {
if(getCurrentPage().getDepth() > 0) {
parentTitle = getCurrentPage().getParent().getTitle();
parentPath = getCurrentPage().getParent().getPath();
}
else {
parentTitle = "No Parent";
parentPath = "No Parent";
}
return ComponentModelBuilder.build(
rcm -> rcm.setHeading(heading),
rcm -> rcm.setParentTitle(parentTitle),
rcm -> rcm.setParentPath(parentPath)
);
}
以下是测试用例:
@Test
public void getModelShouldReturnValidModelWhenInjected() {
Page parentPage = mock(Page.class);
ContentModel contentModel = contentController.getModel();
assertThat(ContentModel instanceof contentModel, is(true));
assertThat(((ContentModel) contentModel).getHeading(), is("Heading For Banner"));
}
这里我们如何模拟currentPage对象并在我的测试用例中用于parentTitle和parentPath。
stacktrace :
java.lang.NullPointerException
at com.prj.project.aem.components.base.BaseController.getCurrentPage(BaseController.java:127)
at com.prj.project.aem.components.content.contentController.getModel(contentController.java:61)