首页 > 解决方案 > 如何进行适当的单元测试?

问题描述

我有方法:

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));
}

这是示例,但我的课程是内部服务,我无法为其创建吸气剂。差异是私有的。如何为此方法进行适当的单元测试?也许架构有问题?

标签: javaunit-testing

解决方案


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/


推荐阅读