I'm coding an application where there's a user API accessible to someone with the right authentication (form authentication). The problem is that runtime exceptions are not displayed in the console nor the client when they in fact happen.
Part of Service:
@Service
@AllArgsConstructor
@Slf4j
public class AppUserService {
private AppUserRepository repository;
public AppUser getUserByUsername(String username) {
if (!repository.existsByUsername(username)) {
log.error("Cannot find user with username: {}", username);
throw new UserNotFoundException("User with username '" + username + "' not found");
}
log.info("Fetching user with username: {}", username);
return repository.findAppUserByUsername(username);
}
}
Unit Test:
@ExtendWith(MockitoExtension.class)
class AppUserServiceTest {
@Mock
private AppUserRepository repository;
private AppUserService underTest;
@BeforeEach
@SuppressWarnings("resource")
void setUp() {
openMocks(this);
underTest = new AppUserService(repository);
}
// [...]
@Test
void cannotGetUserByUsernameWhenUsernameIsNotInDB() {
// given
given(repository.existsByUsername(any())).willReturn(false);
String username = "Adam";
// when
// then
assertThatThrownBy(() -> underTest.getUserByUsername(username))
.isInstanceOf(UserNotFoundException.class)
.hasMessageContaining("User with username '" + username + "' not found");
then(repository).shouldHaveNoMoreInteractions();
}
// [...]
}
Both the unit test and the fact that this log log.error("Cannot find user with username: {}", username); is printed to the console ensure that the throw statement is reached so I guess it's something that has to do with spring
Part of Repository:
@Repository
public interface AppUserRepository extends JpaRepository<AppUser, UUID> {
AppUser findAppUserByUsername(String username);
n // [...]
}
Exception Class:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
}
}
application.properties:
# info
info.application.name=@name@
info.application.version=@version@
info.application.description=@description@
[email protected]@
info.application.groupId=@groupId@
# DB
spring.datasource.url=jdbc:postgresql://localhost:5432/auth
spring.datasource.username=postgres
spring.datasource.password=1234
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create
# server
server.error.include-message=always
server.error.include-binding-errors=always
pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/>
</parent>
<groupId>com.xdavide9</groupId>
<artifactId>Spring-Auth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-Auth</name>
<description>Form based authentication made with Spring Boot.</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I really don't know where this is coming fromn so I included application.properties and pom too. I even tried to create a handler using @ControllerAdvice and @ExceptionHandler and that way I can return a response entity to the client atleast but still nothing to the console.