|
1 | 1 | package com.example.webclientdemo; |
2 | 2 |
|
| 3 | +import com.example.webclientdemo.payload.GithubRepo; |
| 4 | +import com.example.webclientdemo.payload.RepoRequest; |
| 5 | +import org.assertj.core.api.Assertions; |
3 | 6 | import org.junit.Test; |
4 | 7 | import org.junit.runner.RunWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
5 | 9 | import org.springframework.boot.test.context.SpringBootTest; |
| 10 | +import org.springframework.http.MediaType; |
6 | 11 | import org.springframework.test.context.junit4.SpringRunner; |
| 12 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 13 | +import reactor.core.publisher.Mono; |
| 14 | + |
7 | 15 |
|
8 | 16 | @RunWith(SpringRunner.class) |
9 | | -@SpringBootTest |
| 17 | +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
10 | 18 | public class WebclientDemoApplicationTests { |
11 | 19 |
|
| 20 | + @Autowired |
| 21 | + private WebTestClient webTestClient; |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testCreateRepository() { |
| 25 | + RepoRequest repoRequest = new RepoRequest("test-webclient-repository", "Repository created for testing WebClient"); |
| 26 | + |
| 27 | + webTestClient.post().uri("/api/repos") |
| 28 | + .contentType(MediaType.APPLICATION_JSON_UTF8) |
| 29 | + .accept(MediaType.APPLICATION_JSON_UTF8) |
| 30 | + .body(Mono.just(repoRequest), RepoRequest.class) |
| 31 | + .exchange() |
| 32 | + .expectStatus().isOk() |
| 33 | + .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) |
| 34 | + .expectBody() |
| 35 | + .jsonPath("$.name").isNotEmpty() |
| 36 | + .jsonPath("$.name").isEqualTo("test-webclient-repository"); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testGetAllRepositories() { |
| 41 | + webTestClient.get().uri("/api/repos") |
| 42 | + .accept(MediaType.APPLICATION_JSON_UTF8) |
| 43 | + .exchange() |
| 44 | + .expectStatus().isOk() |
| 45 | + .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) |
| 46 | + .expectBodyList(GithubRepo.class); |
| 47 | + } |
| 48 | + |
12 | 49 | @Test |
13 | | - public void contextLoads() { |
| 50 | + public void testGetSingleRepository() { |
| 51 | + webTestClient.get() |
| 52 | + .uri("/api/repos/{repo}", "test-webclient-repository") |
| 53 | + .exchange() |
| 54 | + .expectStatus().isOk() |
| 55 | + .expectBody() |
| 56 | + .consumeWith(response -> |
| 57 | + Assertions.assertThat(response.getResponseBody()).isNotNull()); |
14 | 58 | } |
15 | 59 |
|
| 60 | + @Test |
| 61 | + public void testEditRepository() { |
| 62 | + RepoRequest newRepoDetails = new RepoRequest("updated-webclient-repository", "Updated name and description"); |
| 63 | + webTestClient.patch() |
| 64 | + .uri("/api/repos/{repo}", "test-webclient-repository") |
| 65 | + .contentType(MediaType.APPLICATION_JSON_UTF8) |
| 66 | + .accept(MediaType.APPLICATION_JSON_UTF8) |
| 67 | + .body(Mono.just(newRepoDetails), RepoRequest.class) |
| 68 | + .exchange() |
| 69 | + .expectStatus().isOk() |
| 70 | + .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8) |
| 71 | + .expectBody() |
| 72 | + .jsonPath("$.name").isEqualTo("updated-webclient-repository"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testDeleteRepository() { |
| 77 | + webTestClient.delete() |
| 78 | + .uri("/api/repos/{repo}", "updated-webclient-repository") |
| 79 | + .exchange() |
| 80 | + .expectStatus().isOk(); |
| 81 | + } |
16 | 82 | } |
0 commit comments