[英]Can't attach RestAssured's RQ/RS of Spring Cloud Contract's tests to Allure report
I've successfully added Allure2 to my project with Spring Cloud Contract tests (it uses JUnit5), but tab "Overview" is blank in all successed test of report.我已经通过 Spring Cloud Contract 测试(它使用 JUnit5)成功地将 Allure2 添加到我的项目中,但是在所有成功的报告测试中选项卡“Overview”都是空白的。
I created a listener class, that gets RQ and RS from RestAssured:我创建了一个侦听器 class,它从 RestAssured 获取 RQ 和 RS:
public class JunitListener extends RunListener {
public ByteArrayOutputStream request = new ByteArrayOutputStream();
public ByteArrayOutputStream response = new ByteArrayOutputStream();
public PrintStream requestVar = new PrintStream(request, true);
public PrintStream responseVar = new PrintStream(response, true);
@Override
public void testStarted(Description description) throws Exception {
RestAssured.filters(new ResponseLoggingFilter(LogDetail.ALL, responseVar),
new RequestLoggingFilter(LogDetail.ALL, requestVar));
}
@Override
public void testFinished(Description description) throws Exception {
logRequest(request);
logResponse(response);
}
@Attachment(value = "Client RQ", type = "plain/text", fileExtension = ".log")
public byte[] logRequest(ByteArrayOutputStream stream) {
return attach(stream);
}
@Attachment(value = "Client RS", type = "plain/text", fileExtension = ".log")
public byte[] logResponse(ByteArrayOutputStream stream) {
return attach(stream);
}
public byte[] attach(ByteArrayOutputStream log) {
byte[] array = log.toByteArray();
log.reset();
return array;
}
}
And runner class, that uses listener class:跑步者 class,它使用监听器 class:
public class JunitRunner extends BlockJUnit4ClassRunner {
public JunitListener junitListener;
public JunitRunner(Class<?> klass) throws InitializationError {
super(klass);
junitListener = new JunitListener();
}
@Override
public void run(RunNotifier notifier) {
notifier.addListener(junitListener);
super.run(notifier);
}
}
And then I've added runner class to my base test class:然后我将跑步者 class 添加到我的基础测试 class 中:
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = "server.port=0"
)
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@RunWith(JunitRunner.class)
@AutoConfigureWireMock(port = 8081)
@ActiveProfiles("test")
public abstract class BaseTest {
@LocalServerPort
int localPort;
@BeforeEach
public void setUp(TestInfo testInfo, RestDocumentationContextProvider restDocumentation) {
RestAssured.baseURI = "http://localhost";
RestAssured.port = localPort;
final RequestSpecification idx = new RequestSpecBuilder()
.setBaseUri("http://localhost")
.setPort(localPort)
.addFilter(documentationConfiguration(restDocumentation))
.build();
RestAssured.requestSpecification =
idx.filter(document("contract/" + testInfo.getTestMethod().orElseThrow().getName()));
}
@AfterEach
public void tearDown() {
RestAssured.reset();
}
}
But no record was added to my Allure report and, as I see in debugger, content of listener and runner is never used:( What am I doing wrong?但是我的 Allure 报告中没有添加任何记录,而且正如我在调试器中看到的那样,从未使用过侦听器和运行器的内容:(我做错了什么?
I learned that SCC does not use JUnit to run tests, but uses SpringBootTest.我了解到SCC并没有使用JUnit来跑测试,而是使用了SpringBootTest。
So I've created a SBT listener class:所以我创建了一个 SBT 监听器 class:
public class CustomTestExecutionListener implements TestExecutionListener, Ordered {
public ByteArrayOutputStream request = new ByteArrayOutputStream();
public ByteArrayOutputStream response = new ByteArrayOutputStream();
public PrintStream requestVar = new PrintStream(request, true);
public PrintStream responseVar = new PrintStream(response, true);
public void beforeTestMethod(TestContext testContext) throws Exception {
RestAssured.filters(new ResponseLoggingFilter(LogDetail.ALL, responseVar),
new RequestLoggingFilter(LogDetail.ALL, requestVar));
}
public void afterTestMethod(TestContext testContext) throws Exception {
logRequest(request);
logResponse(response);
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
@Attachment(value = "Client RQ", type = "text/html")
public byte[] logRequest(ByteArrayOutputStream stream) {
return attach(stream);
}
@Attachment(value = "Client RS", type = "text/html")
public byte[] logResponse(ByteArrayOutputStream stream) {
return attach(stream);
}
public byte[] attach(ByteArrayOutputStream log) {
byte[] array = log.toString().getBytes(StandardCharsets.UTF_8);
log.reset();
return array;
}
}
And added that to my SCC base test class:并将其添加到我的 SCC 基础测试 class 中:
@TestExecutionListeners(
value = { CustomTestExecutionListener.class },
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
Now it works and I have 2 log sections in my Allure report.现在它可以工作了,我的 Allure 报告中有 2 个日志部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.