Java Can';t连接重新加载的';Spring Cloud合同的s RQ/RS';美国的诱惑测试报告

Java Can';t连接重新加载的';Spring Cloud合同的s RQ/RS';美国的诱惑测试报告,java,junit5,rest-assured,allure,spring-cloud-contract,Java,Junit5,Rest Assured,Allure,Spring Cloud Contract,我已经通过SpringCloudContract测试成功地将Allure2添加到我的项目中(它使用JUnit5),但在所有成功的报告测试中,“Overview”选项卡都是空白的 我创建了一个listener类,它从Restassed获取RQ和RS: public class JunitListener extends RunListener { public ByteArrayOutputStream request = new ByteArrayOutputStream();

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

我创建了一个listener类,它从Restassed获取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;
    }
}
以及使用侦听器类的runner类:

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

但我的Allure报告中并没有添加任何记录,正如我在debugger中看到的,listener和runner的内容从未被使用:(我做错了什么?

我了解到SCC并没有使用JUnit来运行测试,而是使用SpringBootTest

因此,我创建了一个SBT侦听器类:

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);
    }
}
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基本测试类中:

@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();
    }
}
@TestExecutionListeners(
        value = { CustomTestExecutionListener.class },
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
现在它工作了,我的诱惑报告中有两个日志部分