无法将 RestAssured 的 RQ/RS 项共 Spring 项 Cloud Contract 的测试附加到 Allure 报告

Can't attach RestAssured's RQ/RS of Spring Cloud Contract's tests to Allure report

我已经通过 Spring Cloud Contract 测试(它使用 JUnit5)成功地将 Allure2 添加到我的项目中,但是在所有成功的报告测试中“概述”选项卡都是空白的。

我创建了一个侦听器 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;
    }
}

和运行器 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);
    }
}

然后我将跑步者 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();
    }
}

但是我的 Allure 报告中没有添加任何记录,而且正如我在调试器中看到的那样,从未使用过侦听器和运行器的内容:(我做错了什么?

我了解到 SCC 不使用 JUnit 运行 测试,而是使用 SpringBootTest。

所以我创建了一个 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;
    }
}

并将其添加到我的 SCC 基础测试中 class:

@TestExecutionListeners(
        value = { CustomTestExecutionListener.class },
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)

现在可以使用了,我的 Allure 报告中有 2 个日志部分。