AssertJ - 链接在一起 returns 并提取
AsserJ - chaing together returns and extracting
有时声纳云会抱怨测试中的断言数量。我想知道如何将断言一个接一个地链接起来,但有一种情况我无法弄清楚如何将断言链接起来。
import java.util.List;
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import lombok.Builder;
import lombok.Data;
import static org.assertj.core.api.Assertions.assertThat;
class FakeTest
{
@Test
void fakeTest()
{
// given
var resStatus = "FINISHED";
var tc1 = "Test case 1";
var r1 = "GOOD";
var tc2 = "Test case 2";
var r2 = "ALARMING";
var res1 = Response.builder()
.status(resStatus)
.testCaseName(tc1)
.result(r1)
.build();
var res2 = Response.builder()
.status(resStatus)
.testCaseName(tc2)
.result(r2)
.build();
var result = Result.builder()
.responses(List.of(res1, res2))
.value("PASSED")
.build();
// then
assertThat(result)
.returns("PASSED", Result::getValue);
assertThat(result.getResponses())
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
}
}
@Data
@Builder
class Result
{
private String value;
private List<Response> responses;
}
@Data
@Builder
class Response
{
private String status;
private String testCaseName;
private String result;
private String error;
}
我如何在 FakeTest#fakeTest 中做断言,将它们链接起来而不是分成两个断言(一个用于 assertThat(result).returns(...) 然后 assertThat(result.getResponses ()).提取(...)...)
我想要实现的是:
assertThat(result)
.returns("PASSED", Result::getValue)
.extracting(Result::getResponses) // connection between two assertThat(...)
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
我发现返回的断言对象是不同的:
- ListAssert
在执行第二个断言链时
- AbstractObjectAssert
> 在做 merged assertion 时
由于 java 泛型限制,extracting(singleParam or lambda)
无法 return 断言特定于提取的值类型,在这种情况下,可以使用 [=12 通知 AssertJ 该类型=] 看起来像:
static import org.assertj.core.api.InstanceOfAssertFactories.LIST;
assertThat(result)
.returns("PASSED", Result::getValue)
.extracting(Result::getResponses).asInstanceOf(LIST) // static import for readability
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
InstanceOfAssertFactories 提供 AssertJ 支持断言的类型。
另一种选择是传递直接提取响应及其字段的 lambda(但这可能有点太多工作)。
有时声纳云会抱怨测试中的断言数量。我想知道如何将断言一个接一个地链接起来,但有一种情况我无法弄清楚如何将断言链接起来。
import java.util.List;
import org.assertj.core.groups.Tuple;
import org.junit.jupiter.api.Test;
import lombok.Builder;
import lombok.Data;
import static org.assertj.core.api.Assertions.assertThat;
class FakeTest
{
@Test
void fakeTest()
{
// given
var resStatus = "FINISHED";
var tc1 = "Test case 1";
var r1 = "GOOD";
var tc2 = "Test case 2";
var r2 = "ALARMING";
var res1 = Response.builder()
.status(resStatus)
.testCaseName(tc1)
.result(r1)
.build();
var res2 = Response.builder()
.status(resStatus)
.testCaseName(tc2)
.result(r2)
.build();
var result = Result.builder()
.responses(List.of(res1, res2))
.value("PASSED")
.build();
// then
assertThat(result)
.returns("PASSED", Result::getValue);
assertThat(result.getResponses())
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
}
}
@Data
@Builder
class Result
{
private String value;
private List<Response> responses;
}
@Data
@Builder
class Response
{
private String status;
private String testCaseName;
private String result;
private String error;
}
我如何在 FakeTest#fakeTest 中做断言,将它们链接起来而不是分成两个断言(一个用于 assertThat(result).returns(...) 然后 assertThat(result.getResponses ()).提取(...)...)
我想要实现的是:
assertThat(result)
.returns("PASSED", Result::getValue)
.extracting(Result::getResponses) // connection between two assertThat(...)
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
我发现返回的断言对象是不同的:
- ListAssert
在执行第二个断言链时 - AbstractObjectAssert
> 在做 merged assertion 时
extracting(singleParam or lambda)
无法 return 断言特定于提取的值类型,在这种情况下,可以使用 [=12 通知 AssertJ 该类型=] 看起来像:
static import org.assertj.core.api.InstanceOfAssertFactories.LIST;
assertThat(result)
.returns("PASSED", Result::getValue)
.extracting(Result::getResponses).asInstanceOf(LIST) // static import for readability
.extracting("status", "testCaseName", "result")
.contains(
Tuple.tuple(resStatus, tc1, r1),
Tuple.tuple(resStatus, tc1, r2));
InstanceOfAssertFactories 提供 AssertJ 支持断言的类型。
另一种选择是传递直接提取响应及其字段的 lambda(但这可能有点太多工作)。