Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为Spring WebTestClient指定特定的端口号_Spring_Spring Webclient_Webtestclient - Fatal编程技术网

如何为Spring WebTestClient指定特定的端口号

如何为Spring WebTestClient指定特定的端口号,spring,spring-webclient,webtestclient,Spring,Spring Webclient,Webtestclient,我有一个本地运行的rest端点,我正试图使用SpringWebClient与之通信。作为测试的第一步,我尝试使用SpringWebTestClient。我的本地rest端点在特定端口上运行(比如8068)。我的假设是,由于端口是固定的,我应该使用: SpringBootTest.WebEnvironment.DEFINED_PORT ,然后以某种方式指定代码中的端口。但是我不知道怎么做。它似乎默认为8080。以下是我的代码的重要部分: @RunWith(SpringRunner.class)

我有一个本地运行的rest端点,我正试图使用SpringWebClient与之通信。作为测试的第一步,我尝试使用SpringWebTestClient。我的本地rest端点在特定端口上运行(比如8068)。我的假设是,由于端口是固定的,我应该使用:

SpringBootTest.WebEnvironment.DEFINED_PORT
,然后以某种方式指定代码中的端口。但是我不知道怎么做。它似乎默认为8080。以下是我的代码的重要部分:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SpringWebclientApplicationTests {

@Autowired
private WebTestClient webTestClient;

@Test
public void wcTest() throws Exception {

    String fullUri = "/services/myEndpoint/v1?userIds=jsmith&objectType=DEFAULT";

    WebTestClient.ResponseSpec responseSpec1 = webTestClient.get().uri(fullUri, "").exchange().expectStatus().isOk();
}
此测试预期返回“200OK”,但返回“404Not_FOUND”。错误响应中显示的请求是:

GET http://localhost:8080/services/myEndpoint/v1?userIds=jsmith&objectType=DEFAULT

,显然是因为它默认为8080,我需要将它设置为8068。我非常感谢任何能够解释正确定义端口的人。谢谢。

我想出来了。我认为你不应该使用

SpringBootTest.WebEnvironment.DEFINED_PORT
除非端点正在侦听8080。在我的情况下,由于我需要使用无法控制的端口号,我改为:

@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SpringWebclientApplicationTests {

    private WebTestClient client;

    @Before
    public void setup() {
        String baseUri = "http://localhost:" + "8079";
        this.client = WebTestClient.bindToServer().baseUrl(baseUri).build();
    }

    @Test
    public void wcTest() throws Exception {

    String fullUri = "/services/myEndpoint/v1?userIds=jsmith&objectType=DEFAULT";
    WebTestClient.ResponseSpec responseSpec1 = client.get().uri(fullUri, "").exchange().expectStatus().isOk();
    }
}
,其中我使用bindToServer方法在本地实例化webtestclient,而不是作为自动连线bean,并删除:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
,所以它现在可以正常工作了