Skip to content

Commit 901d7cd

Browse files
author
Rajeev Kumar Singh
committed
Test Order
1 parent f1519a2 commit 901d7cd

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/main/java/com/example/webclientdemo/GithubClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,17 @@
2323
public class GithubClient {
2424
private static final String GITHUB_V3_MIME_TYPE = "application/vnd.github.v3+json";
2525
private static final String GITHUB_API_BASE_URL = "https://api.github.com";
26+
private static final String USER_AGENT = "Spring 5 WebClient";
2627
private static final Logger logger = LoggerFactory.getLogger(GithubClient.class);
2728

2829
private final WebClient webClient;
2930

3031
@Autowired
3132
public GithubClient(AppProperties appProperties) {
32-
3333
this.webClient = WebClient.builder()
3434
.baseUrl(GITHUB_API_BASE_URL)
3535
.defaultHeader(HttpHeaders.CONTENT_TYPE, GITHUB_V3_MIME_TYPE)
36+
.defaultHeader(HttpHeaders.USER_AGENT, USER_AGENT)
3637
.filter(ExchangeFilterFunctions
3738
.basicAuthentication(appProperties.getGithub().getUsername(),
3839
appProperties.getGithub().getToken()))
@@ -43,10 +44,10 @@ public GithubClient(AppProperties appProperties) {
4344

4445
public Flux<GithubRepo> listGithubRepositories() {
4546
return webClient.get()
46-
.uri("/user/repos?sort={sortField}&direction={sortDirection}", "updated", "desc")
47+
.uri("/user/repos?sort={sortField}&direction={sortDirection}",
48+
"updated", "desc")
4749
.exchange()
4850
.flatMapMany(clientResponse -> clientResponse.bodyToFlux(GithubRepo.class));
49-
5051
}
5152

5253
public Mono<GithubRepo> createGithubRepository(RepoRequest repoRequest) {
@@ -79,7 +80,6 @@ public Mono<Void> deleteGithubRepository(String owner, String repo) {
7980
.bodyToMono(Void.class);
8081
}
8182

82-
8383
private ExchangeFilterFunction logRequest() {
8484
return (clientRequest, next) -> {
8585
logger.info("Request: {} {}", clientRequest.method(), clientRequest.url());
@@ -88,5 +88,4 @@ private ExchangeFilterFunction logRequest() {
8888
return next.exchange(clientRequest);
8989
};
9090
}
91-
9291
}

src/main/java/com/example/webclientdemo/GithubController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public Mono<Void> deleteGithubRepository(@PathVariable String repo) {
5757

5858
@ExceptionHandler(WebClientResponseException.class)
5959
public ResponseEntity<String> handleWebClientResponseException(WebClientResponseException ex) {
60-
logger.error("Error from WebClient - Status {}, Body {}", ex.getRawStatusCode(), ex.getResponseBodyAsString(), ex);
60+
logger.error("Error from WebClient - Status {}, Body {}", ex.getRawStatusCode(),
61+
ex.getResponseBodyAsString(), ex);
6162
return ResponseEntity.status(ex.getRawStatusCode()).body(ex.getResponseBodyAsString());
6263
}
6364
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
app.github.username=
2-
app.github.token=
2+
app.github.token=

src/test/java/com/example/webclientdemo/WebclientDemoApplicationTests.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import com.example.webclientdemo.payload.GithubRepo;
44
import com.example.webclientdemo.payload.RepoRequest;
55
import org.assertj.core.api.Assertions;
6+
import org.junit.FixMethodOrder;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
9+
import org.junit.runners.MethodSorters;
810
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.boot.test.context.SpringBootTest;
1012
import org.springframework.http.MediaType;
@@ -15,13 +17,14 @@
1517

1618
@RunWith(SpringRunner.class)
1719
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
20+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
1821
public class WebclientDemoApplicationTests {
1922

2023
@Autowired
2124
private WebTestClient webTestClient;
2225

2326
@Test
24-
public void testCreateGithubRepository() {
27+
public void test1CreateGithubRepository() {
2528
RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient");
2629

2730
webTestClient.post().uri("/api/repos")
@@ -37,7 +40,7 @@ public void testCreateGithubRepository() {
3740
}
3841

3942
@Test
40-
public void testGetAllGithubRepositories() {
43+
public void test2GetAllGithubRepositories() {
4144
webTestClient.get().uri("/api/repos")
4245
.accept(MediaType.APPLICATION_JSON_UTF8)
4346
.exchange()
@@ -47,7 +50,7 @@ public void testGetAllGithubRepositories() {
4750
}
4851

4952
@Test
50-
public void testGetSingleGithubRepository() {
53+
public void test3GetSingleGithubRepository() {
5154
webTestClient.get()
5255
.uri("/api/repos/{repo}", "test-webclient-repository")
5356
.exchange()
@@ -58,7 +61,7 @@ public void testGetSingleGithubRepository() {
5861
}
5962

6063
@Test
61-
public void testEditGithubRepository() {
64+
public void test4EditGithubRepository() {
6265
RepoRequest newRepoDetails = new RepoRequest("updated-webclient-repository", "Updated name and description");
6366
webTestClient.patch()
6467
.uri("/api/repos/{repo}", "test-webclient-repository")
@@ -73,7 +76,7 @@ public void testEditGithubRepository() {
7376
}
7477

7578
@Test
76-
public void testDeleteGithubRepository() {
79+
public void test5DeleteGithubRepository() {
7780
webTestClient.delete()
7881
.uri("/api/repos/{repo}", "updated-webclient-repository")
7982
.exchange()

0 commit comments

Comments
 (0)