如何在 JUnit 测试中将 MockWebServer 端口设置为 WebClient?

mem*_*und 8 java spring spring-boot mockwebserver okhttp3

我正在使用spring-bootwith WebClient,它作为 bean 自动装配。

问题:在编写junit集成测试时,我必须使用 okhttp MockWebServer。此模拟始终在随机端口上启动,例如localhost:14321.

现在我WebClient当然有一个固定的网址,它将请求发送到。该 url 可能由application.properties类似的参数给出webclient.url=https://my.domain.com/,因此我可以在junit测试中覆盖该字段。但只是静态的。

问题:如何WebClient在 a 中重置bean,@SpringBootTest以便它始终将请求发送到我的模拟服务器?

@Service
public class WebClientService {
     public WebClientService(WebClient.Builder builder, @Value("${webclient.url}" String url) {
          this.webClient = builder.baseUrl(url)...build();
     }

     public Response send() {
          return webClient.body().exchange().bodyToMono();
     }
}

@Service
public void CallingService {
      @Autowired
      private WebClientService service;

      public void call() {
           service.send();
      }
}


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MyWebTest {
        @Autowired
        private CallingService calling;

        @Test
        public void test() {
             MockWebServer mockWebServer = new MockWebServer();
             System.out.println("Current mock server url: " + mockWebServer.url("/").toString()); //this is random    

             mockWebServer.enqueue(new MockResponse()....);

             //TODO how to make the mocked server url public to the WebClient?
             calling.call();
        }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我正在编写一个完整的真实世界 junit 集成测试。唯一的问题是:如何将MockWebServerurl 和端口传递给WebClient以便它自动将请求发送到我的模拟?

旁注:我在MockWebServer这里绝对需要一个随机端口,以免干扰其他正在运行的测试或应用程序。因此必须坚持随机端口,并找到一种方法将其传递给 web 客户端(或动态覆盖应用程序属性)。


更新:我想出了以下方法,这很有效。但也许有人知道如何使 mockserver 字段非静态

@ContextConfiguration(initializers = RandomPortInitializer.class)
public abstract class AbstractITest {
    @ClassRule
    public static final MockWebServer mockWebServer = new MockWebServer();

    public static class RandomPortInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        @Override
        public void initialize(ConfigurableApplicationContext applicationContext) {
            TestPropertySourceUtils.addInlinedPropertiesToEnvironment(applicationContext,
                    "webclient.url=" + mockWebServer.url("/").toString());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mag*_*ter 12

从 Spring Framework 5.2.5 (Spring Boot 2.x) 开始,您可以使用DynamicPropertySource非常方便的注释。

这是一个完整的示例,您可以使用它MockWebServer来绑定正确的端口:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class AbstractIT {

    static MockWebServer mockWebServer;

    @DynamicPropertySource
    static void properties(DynamicPropertyRegistry r) throws IOException {
        r.add("some-service.url", () -> "http://localhost:" + mockWebServer.getPort());
    }

    @BeforeAll
    static void beforeAll() throws IOException {
        mockWebServer = new MockWebServer();
        mockWebServer.start();
    }

    @AfterAll
    static void afterAll() throws IOException {
        mockWebServer.shutdown();
    }
}
Run Code Online (Sandbox Code Playgroud)