java 如何启用Spring Cloud Contract Wiremock服务器日志?

bttbmeg0  于 2022-12-28  发布在  Java
关注(0)|答案(1)|浏览(198)

根据官方文档How to Debug,我需要在属性文件中添加以下内容以启用日志记录

logging.level.org.apache.http.wire=DEBUG
logging.level.com.github.tomakehurst.wiremock=DEBUG

但是,当请求发送到wirerock服务器时,我在控制台上看不到任何日志。我只能获得特定平台的500 Internal Server Error。因此,为了排除故障,我尝试在请求到达wirerock服务器时拦截活动。
在我的pom文件里

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-webflux</artifactId>
  <scope>test</scope>
</dependency>

我还尝试添加logback-test.xml文件与以下内容。但是,这是不记录任何东西以及。

<logger name="com.github.tomakehurst.wiremock" level="DEBUG"/> 
<logger name="wiremock.org" level="DEBUG"/> 
<logger name="WireMock" level="DEBUG"/> 
<logger name="/" level="DEBUG"/>

我的测试课

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
    classes = MyApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(
    locations = "/application-integration-test.properties"
)
@AutoConfigureWireMock(port = 0)
@AutoConfigureWebTestClient(timeout = "100000")
public class MyApplicationTest {

  private WebTestClient client;

  @LocalServerPort
  private int port;

  @Before
  public void setUp() throws Exception {

    client = WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build();
  }

  /// Test code 

}

有人能告诉我我缺了什么吗?

ttcibm8c

ttcibm8c1#

如果您正在通过Spring注解@AutoConfigureWireMock使用WireMock,在测试控制台输出中查看WireMock(详细)日志的最佳方法应该是在您的测试类中添加以下bean:

@TestConfiguration
static class WireMockTestConfiguration {
    @Bean
    WireMockConfigurationCustomizer optionsCustomizer() {
        return config -> config.notifier(new ConsoleNotifier(true));
    }
}

您可以使用@ImportWireMockTestConfiguration添加到Spring上下文。

相关问题