Skip to content

Commit 93726ac

Browse files
committed
Add native test for @HttpExchange
Closes spring-projectsgh-139
1 parent 0af95ab commit 93726ac

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

webclient/src/aotSmokeTest/java/com/example/webclient/WebClientApplicationAotTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ void httpsWorks(AssertableOutput output) {
3131
});
3232
}
3333

34+
@Test
35+
void serviceWorks(AssertableOutput output) {
36+
Awaitility.await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> {
37+
assertThat(output).hasSingleLineContaining(
38+
"service: ExchangeDataDto{url='https://httpbin.org/anything', method='GET'}");
39+
});
40+
}
41+
3442
}

webclient/src/main/java/com/example/webclient/CLR.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ class CLR implements CommandLineRunner {
1414

1515
private final Builder webClientBuilder;
1616

17-
CLR(WebClient.Builder webClientBuilder) {
17+
private final DataService dataService;
18+
19+
CLR(WebClient.Builder webClientBuilder, DataService dataService) {
1820
this.webClientBuilder = webClientBuilder;
21+
this.dataService = dataService;
1922
}
2023

2124
@Override
2225
public void run(String... args) {
2326
http();
2427
https();
28+
service();
2529
}
2630

2731
private void http() {
@@ -50,4 +54,15 @@ private void https() {
5054
}
5155
}
5256

57+
private void service() {
58+
try {
59+
ExchangeDataDto dto = this.dataService.getData();
60+
System.out.printf("service: %s%n", dto);
61+
}
62+
catch (Exception ex) {
63+
System.out.println("service failed:");
64+
ex.printStackTrace(System.out);
65+
}
66+
}
67+
5368
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.webclient;
2+
3+
import org.springframework.web.service.annotation.GetExchange;
4+
5+
interface DataService {
6+
7+
@GetExchange("/anything")
8+
ExchangeDataDto getData();
9+
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.webclient;
2+
3+
public class ExchangeDataDto {
4+
5+
private String url;
6+
7+
private String method;
8+
9+
public ExchangeDataDto() {
10+
}
11+
12+
public String getUrl() {
13+
return url;
14+
}
15+
16+
public void setUrl(String url) {
17+
this.url = url;
18+
}
19+
20+
public String getMethod() {
21+
return method;
22+
}
23+
24+
public void setMethod(String method) {
25+
this.method = method;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "ExchangeDataDto{" + "url='" + url + '\'' + ", method='" + method + '\'' + '}';
31+
}
32+
33+
}

webclient/src/main/java/com/example/webclient/WebClientApplication.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.WebApplicationType;
55
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.web.reactive.function.client.WebClient;
8+
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
9+
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
610

711
@SpringBootApplication
812
public class WebClientApplication {
@@ -14,4 +18,12 @@ public static void main(String[] args) throws InterruptedException {
1418
Thread.currentThread().join(); // To be able to measure memory consumption
1519
}
1620

21+
@Bean
22+
DataService dataService(WebClient.Builder webClientBuilder) {
23+
WebClient webClient = webClientBuilder.baseUrl("https://httpbin.org/").build();
24+
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
25+
.build();
26+
return factory.createClient(DataService.class);
27+
}
28+
1729
}

0 commit comments

Comments
 (0)