java - 如何进行适当的单元测试?
问题描述
我有方法:
public ReportBuilder addDiffs(List<String> diffs) {
if (this.diffs == null) {
this.diffs = new ArrayList<>();
}
this.diffs.addAll(diffs);
return this;
}
我想做单元测试。据我所知,它应该是这样的:
@Test
public void ReportBuilderTest() {
ReportBuilder builder = new ReportBuilder();
List<String> list = new ArrayList<>();
list.add("some string");
builder.addDiffs(list);
assertEquals(builder.getDiffs(), list));
}
这是示例,但我的课程是内部服务,我无法为其创建吸气剂。差异是私有的。如何为此方法进行适当的单元测试?也许架构有问题?
解决方案
You can use the Mockito framework. The object you are going to use can be built with dummy values using @mock Annotation. For more information on the different annotations, check the website https://site.mockito.org/
推荐阅读
- ios - 安装 xcode 10 beta 后 Xcode 9 模拟器无法工作
- c - 使 b+tree 并发 (c)
- java - JFileChooser 在 main 方法之外不起作用
- php - 如何在 PHP 中从具有多个同名项目的数组中正确生成 SOAP WSDL 请求?
- sql - 根据一行计算多行
- json - 通过 url 搜索 JSON 数据
- canvas - 如何发现要在 Canvas 上绘制的点的位置?
- asp.net-mvc - 我应该如何在 C# 中由 SmtpClient 发送的电子邮件中呈现 HTML
- c# - 如何检查标志是否为假/真,然后决定是否在 OnGUI 中启用假/真按钮?
- scala - 使用 scala 中同一包的特征中定义的类。