unit-testing - aem 的单元测试不涵盖条件
问题描述
我是单元测试的新手。我正在将 junit5 用于 aem 6.5 应用程序。我正在尝试编写单元测试,以便我可以迁移到需要至少 50% 单元测试的云,这就是我现在在做什么。我的代码有一个 getTags() 的 init 方法。我的单元测试工作正常,但 jacoco 报告了一条未被覆盖的行。任何帮助将不胜感激,让我朝着正确的方向前进。
我有这堂课:
@Self
private SlingHttpServletRequest request;
@ValueMapValue
private List<String> tags;
@Override
public String getExportedType() {
return RESOURCE_TYPE;
}
@PostConstruct
public void init() {
Resource tagsNode = request.getResource().getChild("tags");
tags = new ArrayList<>();
if (tagsNode!=null) {
for (Resource child : tagsNode.getChildren()) {
TagModel tag = child.adaptTo(TagModel.class);
tags.add(tag.getTag());
}
}
}
public List<String> getTags() {
return this.tags;
}
我有这个针对 aem 的单元测试
@Test
void testGetTags() {
List<String> expected = new ImmutableList.Builder<String>()
.add("4")
.add("22")
.build();
ctx.currentResource("/content/blog_placeholder");
BlogPlaceholderModel blogPlaceholderModel = ctx.request().adaptTo(BlogPlaceholderModel.class);
List<String> actual = blogPlaceholderModel.getTags();
assertEquals(expected, actual);
}
@Test
void testGetExportedType() {
final String expected = "furniture-row/components/content/blog-placeholder";
ctx.currentResource("/content/blog_placeholder");
BlogPlaceholderModel blogPlaceholderModel = ctx.request().adaptTo(BlogPlaceholderModel.class);
String actual = blogPlaceholderModel.getExportedType();
assertEquals(expected, actual);
}
出于某种原因,我的测试没有涵盖 if 条件:
我错过了什么?谢谢
解决方案
您的测试部分覆盖了这条线。
您确实对这种情况进行了测试,(tagsNode != null) == true
但对(tagsNode != null) == false
. 因此,该行是黄色的,表示并未if
涵盖该语句的所有“分支”。
推荐阅读
- swiftui - 如何在 Swift UI 文本字段中设置位置自动完成?
- php - 出勤表列标题中的日期
- powershell - 执行时的powershell脚本给了我执行策略更改对话框
- database - 我们可以在 mongo DB 上使用弹性搜索吗?
- angular - 如何在 Angular/Typescript 中更新 KeyValue 数组的值
- swift - 如何为具有固定大小的 Watch OS 创建可滚动列表?
- javascript - 根据屏幕大小一一显示/隐藏过滤器
- python - 如何使用 pandas 遍历每一行中的单词
- python - 为 __le__ 和 __ge__ 键入别名
- reactjs - 如何在 React 中使用 RecoilJS 管理大型数据集?