gpt4 book ai didi

unit-testing - 单元测试 Java 8 谓词

转载 作者:行者123 更新时间:2023-12-02 20:53:58 26 4
gpt4 key购买 nike

我有一个这样的 Java 8 谓词。我如何为此编写单元测试

 Predicate<DTO> isDone = (dtO) ->
(!dto.isFinished() &&
!dto.isCompleted());

谢谢

最佳答案

我会这样测试它:

private final Predicate<DTO> isDone = (dto) ->
(!dto.isFinished() && !dto.isCompleted());

@Test
public void a() throws Exception {
// given
DTO dto = new DTO(true, true);

// when
boolean result = isDone.test(dto);

// then
assertThat(result).isFalse();
}

@Test
public void s() throws Exception {
// given
DTO dto = new DTO(true, false);

// when
boolean result = isDone.test(dto);

// then
assertThat(result).isFalse();
}

@Test
public void d() throws Exception {
// given
DTO dto = new DTO(false, true);

// when
boolean result = isDone.test(dto);

// then
assertThat(result).isFalse();
}

@Test
public void f() throws Exception {
// given
DTO dto = new DTO(false, false);

// when
boolean result = isDone.test(dto);

// then
assertThat(result).isTrue();
}

@AllArgsConstructor
public static class DTO {
private final boolean isFinished;
private final boolean isCompleted;

public boolean isFinished() {
return isFinished;
}

public boolean isCompleted() {
return isCompleted;
}
}

代替 asf 正确命名测试,如:should_be_done_when_it_is_both_finished_and_completed

我想 DTO 只是一个值对象,所以我宁愿创建真实实例而不是使用模拟。

关于unit-testing - 单元测试 Java 8 谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41112267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com