Merge pull request #16303 from rezaganjis/master

BAEL-7540 - The difference between throwing an exception and Mono.error() in Spring webflux
This commit is contained in:
Maiklins 2024-04-08 21:59:11 +02:00 committed by GitHub
commit 2a0eb6ef01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package com.baeldung.webflux.exceptionhandeling.ex;
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
public NotFoundException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.webflux.exceptionhandeling.model;
public class User {
private String id;
private String username;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public User(String userId, String userName) {
this.id = userId;
this.username = userName;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.webflux.exceptionhandeling.repository;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.stereotype.Repository;
import com.baeldung.webflux.exceptionhandeling.model.User;
@Repository
public class UserRepository {
private final Map<String, User> userDatabase = new ConcurrentHashMap<>();
public UserRepository() {
userDatabase.put("1", new User("1", "John Doe"));
userDatabase.put("2", new User("2", "Jane Smith"));
}
public User findById(String id) {
return userDatabase.get(id);
}
}

View File

@ -0,0 +1,34 @@
package com.baeldung.webflux.exceptionhandeling.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import com.baeldung.webflux.exceptionhandeling.ex.NotFoundException;
import com.baeldung.webflux.exceptionhandeling.model.User;
import com.baeldung.webflux.exceptionhandeling.repository.UserRepository;
import reactor.core.publisher.Mono;
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public Mono<User> getUserByIdThrowingException(String id) {
User user = userRepository.findById(id);
if (user == null)
throw new NotFoundException("User Not Found");
return Mono.justOrEmpty(user);
}
public Mono<User> getUserByIdUsingMonoError(String id) {
User user = userRepository.findById(id);
return (user != null) ? Mono.justOrEmpty(user) : Mono.error(new NotFoundException("User Not Found"));
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.webflux.exceptionhandeling;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import com.baeldung.webflux.exceptionhandeling.ex.NotFoundException;
import com.baeldung.webflux.exceptionhandeling.model.User;
import com.baeldung.webflux.exceptionhandeling.service.UserService;
import com.baeldung.webflux.exceptionhandeling.repository.UserRepository;
public class UserControllerUnitTest {
UserRepository repositoryMock = mock(UserRepository.class);
private final UserService userService = new UserService(repositoryMock);
@Test
public void givenNonExistUser_whenFailureCall_then_Throws_exception() {
assertThrows(NotFoundException.class, () -> userService.getUserByIdThrowingException("3"));
}
@Test
public void givenNonExistUser_whenFailureCall_then_returnMonoError() {
Mono<User> result = userService.getUserByIdUsingMonoError("3");
StepVerifier.create(result)
.expectError(NotFoundException.class)
.verify();
}
}