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

时间:2021-01-28 15:45:56

标签: java junit5 rest-assured allure spring-cloud-contract

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

我创建了一个监听器类,它从 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;
    }
}

还有使用监听器类的运行器类:

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);
    }
}

然后我在我的基础测试类中添加了 runner 类:

@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 报告中没有添加任何记录,而且正如我在调试器中看到的那样,从未使用过侦听器和运行器的内容 :( 我做错了什么?

1 个答案:

答案 0 :(得分:1)

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

所以我创建了一个 SBT 监听器类:

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

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

<ng-template #menubutton let-link="link" let-icon="icon" let-text="text" ...>  
... does magic :)
</ng-template>

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