我们需要在单元测试中测试 findAll 方法吗?
Do we need to test findAll methods in Unit Tests?
我有以下服务方式:
public List<LabelDTO> findAllByLanguage(Language language) {
final Map<Pair<UUID, String>, List<TextLabelTranslatable>> valueList =
translatableRepository.findAllTranslatable();
// code omitted
return labelDTOList;
}
我正在尝试为此方法创建以下单元测试:
@Test
public void demo_test() {
List<LabelWithTranslatable> labelWithTranslatableList = new ArrayList<>();
LabelWithTranslatable labelWithTranslatable1 = mock(LabelWithTranslatable.class);
labelWithTranslatable1.getEntity().setId(123L);
labelWithTranslatableList.add(labelWithTranslatable1);
LabelWithTranslatable labelWithTranslatable2 = mock(LabelWithTranslatable.class);
labelWithTranslatable2.getEntity().setId(456L);
labelWithTranslatableList.add(labelWithTranslatable2);
// translatableRepository.findAllTranslatable() returns "List<LabelWithTranslatable>"
when(translatableRepository.findAllTranslatable())
.thenReturn(labelWithTranslatableList);
List<LabelDTO> result = labelService.findAllByLanguage(SupportedLanguage.en);
// assertions
}
我的问题是:
1. 我们需要在单元测试中测试 findAll()
方法吗?如果是这样,我们是否应该在列表中创建多个记录,然后将返回的结果与预期的过滤值进行比较?
2.我模拟了LabelWithTranslatable
,但是它有一个实体,它的值为空(labelWithTranslatable1.getEntity()
).那么,我应该如何嘲笑那个实体呢?这是关系:
LabelWithTranslatable:
public interface LabelWithTranslatable extends
GenericTranslatable<Label, LabelTranslatable> {
}
通用翻译:
public interface GenericTranslatable<E extends BaseEntity, T extends BaseTranslatableEntity> {
E getEntity();
T getTranslatable();
}
是的,您确实需要测试您的 findAllXxx
方法 -- 您问题中 //code omitted
注释所代表的代码需要测试范围。
正如您所建议的,您可以通过模拟底层存储库来做到这一点。
要模拟您的 LabelWithTranslatable
实例,您需要执行以下操作:
LabelWithTranslatable labelWithTranslatable1 = mock(LabelWithTranslatable.class);
Label label1 = mock(Label.class);
when(label1.getId()).thenReturn(123L);
when(labelWithTranslatable1.getEntity()).thenReturn(label1);
labelWithTranslatableList.add(labelWithTranslatable1);
我有以下服务方式:
public List<LabelDTO> findAllByLanguage(Language language) {
final Map<Pair<UUID, String>, List<TextLabelTranslatable>> valueList =
translatableRepository.findAllTranslatable();
// code omitted
return labelDTOList;
}
我正在尝试为此方法创建以下单元测试:
@Test
public void demo_test() {
List<LabelWithTranslatable> labelWithTranslatableList = new ArrayList<>();
LabelWithTranslatable labelWithTranslatable1 = mock(LabelWithTranslatable.class);
labelWithTranslatable1.getEntity().setId(123L);
labelWithTranslatableList.add(labelWithTranslatable1);
LabelWithTranslatable labelWithTranslatable2 = mock(LabelWithTranslatable.class);
labelWithTranslatable2.getEntity().setId(456L);
labelWithTranslatableList.add(labelWithTranslatable2);
// translatableRepository.findAllTranslatable() returns "List<LabelWithTranslatable>"
when(translatableRepository.findAllTranslatable())
.thenReturn(labelWithTranslatableList);
List<LabelDTO> result = labelService.findAllByLanguage(SupportedLanguage.en);
// assertions
}
我的问题是:
1. 我们需要在单元测试中测试 findAll()
方法吗?如果是这样,我们是否应该在列表中创建多个记录,然后将返回的结果与预期的过滤值进行比较?
2.我模拟了LabelWithTranslatable
,但是它有一个实体,它的值为空(labelWithTranslatable1.getEntity()
).那么,我应该如何嘲笑那个实体呢?这是关系:
LabelWithTranslatable:
public interface LabelWithTranslatable extends
GenericTranslatable<Label, LabelTranslatable> {
}
通用翻译:
public interface GenericTranslatable<E extends BaseEntity, T extends BaseTranslatableEntity> {
E getEntity();
T getTranslatable();
}
是的,您确实需要测试您的 findAllXxx
方法 -- 您问题中 //code omitted
注释所代表的代码需要测试范围。
正如您所建议的,您可以通过模拟底层存储库来做到这一点。
要模拟您的 LabelWithTranslatable
实例,您需要执行以下操作:
LabelWithTranslatable labelWithTranslatable1 = mock(LabelWithTranslatable.class);
Label label1 = mock(Label.class);
when(label1.getId()).thenReturn(123L);
when(labelWithTranslatable1.getEntity()).thenReturn(label1);
labelWithTranslatableList.add(labelWithTranslatable1);