Skip to content

Commit 8945030

Browse files
committed
add missing code for http interface
1 parent 0c6dfda commit 8945030

File tree

3 files changed

+17
-53
lines changed

3 files changed

+17
-53
lines changed

spring-boot-modules/spring-boot-3-2/pom.xml

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,9 @@
1515
</parent>
1616

1717
<dependencies>
18-
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
19-
<dependency>
20-
<groupId>org.springframework</groupId>
21-
<artifactId>spring-web</artifactId>
22-
<version>6.2.3</version>
23-
</dependency>
24-
<dependency>
25-
<groupId>org.springframework</groupId>
26-
<artifactId>spring-core</artifactId>
27-
<version>6.2.3</version>
28-
</dependency>
2918
<dependency>
3019
<groupId>org.springframework.boot</groupId>
3120
<artifactId>spring-boot-starter-web</artifactId>
32-
<version>3.4.3</version>
3321
</dependency>
3422
<dependency>
3523
<groupId>org.springframework.boot</groupId>
@@ -81,7 +69,6 @@
8169
<dependency>
8270
<groupId>org.projectlombok</groupId>
8371
<artifactId>lombok</artifactId>
84-
<version>${lombok.version}</version>
8572
<optional>true</optional>
8673
</dependency>
8774
<dependency>
@@ -105,7 +92,6 @@
10592
<dependency>
10693
<groupId>org.springframework.boot</groupId>
10794
<artifactId>spring-boot-starter-test</artifactId>
108-
<version>3.4.3</version>
10995
</dependency>
11096
<dependency>
11197
<groupId>org.postgresql</groupId>
@@ -121,14 +107,9 @@
121107
<artifactId>spring-rabbit-test</artifactId>
122108
<scope>test</scope>
123109
</dependency>
124-
<dependency>
125-
<groupId>org.springframework.data</groupId>
126-
<artifactId>spring-data-redis</artifactId>
127-
</dependency>
128110
<dependency>
129111
<groupId>redis.clients</groupId>
130112
<artifactId>jedis</artifactId>
131-
<version>${jedis.version}</version>
132113
<type>jar</type>
133114
</dependency>
134115
<dependency>
@@ -272,8 +253,8 @@
272253

273254
<properties>
274255
<start-class>com.baeldung.restclient.RestClientApplication</start-class>
275-
<mapstruct.version>1.6.0.Beta1</mapstruct.version>
276-
<mockserver.version>5.14.0</mockserver.version>
256+
<mapstruct.version>1.6.0</mapstruct.version>
257+
<mockserver.version>5.15.0</mockserver.version>
277258
<lombok-mapstruct-binding.version>0.2.0</lombok-mapstruct-binding.version>
278259
<jedis.version>5.0.2</jedis.version>
279260
<spring-kafka.version>3.1.2</spring-kafka.version>

spring-boot-modules/spring-boot-3-2/src/main/java/com/baeldung/httpinterface/BooksClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.springframework.stereotype.Component;
44
import org.springframework.web.reactive.function.client.WebClient;
5+
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
56
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
67

78
@Component
@@ -10,8 +11,7 @@ public class BooksClient {
1011
private final BooksService booksService;
1112

1213
public BooksClient(WebClient webClient) {
13-
HttpServiceProxyFactory httpServiceProxyFactory =
14-
HttpServiceProxyFactory.builder()
14+
HttpServiceProxyFactory httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(WebClientAdapter.create(webClient))
1515
.build();
1616
booksService = httpServiceProxyFactory.createClient(BooksService.class);
1717
}
Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package com.baeldung.httpinterface;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.mockito.ArgumentMatchers.anyMap;
5+
import static org.mockito.ArgumentMatchers.anyString;
6+
import static org.mockito.BDDMockito.given;
7+
8+
import java.util.List;
9+
310
import org.junit.jupiter.api.Test;
411
import org.junit.jupiter.api.extension.ExtendWith;
512
import org.mockito.Answers;
@@ -8,28 +15,18 @@
815
import org.mockito.junit.jupiter.MockitoExtension;
916
import org.springframework.core.ParameterizedTypeReference;
1017
import org.springframework.http.HttpMethod;
11-
import org.springframework.http.HttpStatusCode;
12-
import org.springframework.http.ResponseEntity;
1318
import org.springframework.web.reactive.function.client.WebClient;
14-
import reactor.core.publisher.Mono;
1519

16-
import static org.mockito.BDDMockito.*;
17-
18-
import java.util.List;
19-
20-
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import reactor.core.publisher.Mono;
2121

2222
@ExtendWith(MockitoExtension.class)
2323
class BooksServiceMockitoUnitTest {
2424

2525
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
2626
private WebClient webClient;
2727

28-
//@InjectMocks
29-
private BooksClient booksClient;
30-
3128
@InjectMocks
32-
private BooksService booksService;
29+
private BooksClient booksClient;
3330

3431
@Test
3532
void givenMockedWebClientReturnsTwoBooks_whenGetBooksServiceMethodIsCalled_thenListOfTwoBooksIsReturned() {
@@ -41,7 +38,8 @@ void givenMockedWebClientReturnsTwoBooks_whenGetBooksServiceMethodIsCalled_thenL
4138
new Book(1,"Book_1", "Author_1", 1998),
4239
new Book(2, "Book_2", "Author_2", 1999)
4340
)));
44-
// BooksService booksService = booksClient.getBooksService();
41+
42+
BooksService booksService = booksClient.getBooksService();
4543
List<Book> books = booksService.getBooks();
4644
assertEquals(2, books.size());
4745
}
@@ -54,7 +52,7 @@ void givenMockedWebClientReturnsBook_whenGetBookServiceMethodIsCalled_thenBookIs
5452
.bodyToMono(new ParameterizedTypeReference<Book>(){}))
5553
.willReturn(Mono.just(new Book(1,"Book_1", "Author_1", 1998)));
5654

57-
//BooksService booksService = booksClient.getBooksService();
55+
BooksService booksService = booksClient.getBooksService();
5856
Book book = booksService.getBook(1);
5957
assertEquals("Book_1", book.title());
6058
}
@@ -67,24 +65,9 @@ void givenMockedWebClientReturnsBook_whenSaveBookServiceMethodIsCalled_thenBookI
6765
.bodyToMono(new ParameterizedTypeReference<Book>(){}))
6866
.willReturn(Mono.just(new Book(3, "Book_3", "Author_3", 2000)));
6967

70-
//BooksService booksService = booksClient.getBooksService();
68+
BooksService booksService = booksClient.getBooksService();
7169
Book book = booksService.saveBook(new Book(3, "Book_3", "Author_3", 2000));
7270
assertEquals("Book_3", book.title());
7371
}
7472

75-
@Test
76-
void givenMockedWebClientReturnsOk_whenDeleteBookServiceMethodIsCalled_thenOkCodeIsReturned() {
77-
given(webClient.method(HttpMethod.DELETE)
78-
.uri(anyString(), anyMap())
79-
.retrieve()
80-
.toBodilessEntity()
81-
.block(any())
82-
.getStatusCode())
83-
.willReturn(HttpStatusCode.valueOf(200));
84-
85-
//BooksService booksService = booksClient.getBooksService();
86-
ResponseEntity<Void> response = booksService.deleteBook(3);
87-
assertEquals(HttpStatusCode.valueOf(200), response.getStatusCode());
88-
}
89-
9073
}

0 commit comments

Comments
 (0)