Skip to content

Commit 35a0189

Browse files
committed
test: add unit tests for AuthController
1 parent f979433 commit 35a0189

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package fun.trackmoney.auth.dto;
22

3-
public record LoginResponseDTO (String Token){
3+
public record LoginResponseDTO (String token){
44
}

src/main/java/fun/trackmoney/utils/response/ApiResponse.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ public void setSuccess(boolean success) {
5858
this.success = success;
5959
}
6060

61+
public boolean getSuccess() {
62+
return success;
63+
}
64+
65+
6166
/**
6267
* Gets the message of the response.
6368
*
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package fun.trackmoney.auth.controller;
2+
3+
import fun.trackmoney.auth.dto.LoginRequestDTO;
4+
import fun.trackmoney.auth.dto.LoginResponseDTO;
5+
import fun.trackmoney.auth.service.AuthService;
6+
import fun.trackmoney.user.dtos.UserRequestDTO;
7+
import fun.trackmoney.user.dtos.UserResponseDTO;
8+
import fun.trackmoney.utils.response.ApiResponse;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.InjectMocks;
12+
import org.mockito.Mock;
13+
import org.mockito.MockitoAnnotations;
14+
import org.springframework.http.ResponseEntity;
15+
import java.util.UUID;
16+
import static org.junit.jupiter.api.Assertions.*;
17+
import static org.mockito.Mockito.when;
18+
19+
class AuthControllerTest {
20+
21+
@Mock
22+
AuthService authService;
23+
@InjectMocks
24+
AuthController authController;
25+
26+
@BeforeEach
27+
void setUp() {
28+
MockitoAnnotations.openMocks(this);
29+
}
30+
31+
@Test
32+
void shouldReturnUserOnRegister() {
33+
UserRequestDTO userRequest = new UserRequestDTO("John","john@example.com", "123");
34+
UserResponseDTO userResponse = new UserResponseDTO(UUID.randomUUID(), "John", "john@example.com");
35+
36+
when(authService.register(userRequest)).thenReturn(userResponse);
37+
38+
ResponseEntity<ApiResponse<UserResponseDTO>> response = authController.register(userRequest);
39+
40+
assertTrue(response.getBody().getSuccess());
41+
assertEquals("User register!", response.getBody().getMessage());
42+
assertEquals("John", response.getBody().getData().name());
43+
}
44+
45+
@Test
46+
void shouldReturnTokenOnLogin() {
47+
LoginRequestDTO loginRequest = new LoginRequestDTO("john@example.com", "123");
48+
LoginResponseDTO loginResponse = new LoginResponseDTO("jwt-token");
49+
50+
when(authService.login(loginRequest)).thenReturn(loginResponse);
51+
52+
ResponseEntity<ApiResponse<LoginResponseDTO>> response = authController.login(loginRequest);
53+
54+
assertTrue(response.getBody().getSuccess());
55+
assertEquals("Login successful!", response.getBody().getMessage());
56+
assertEquals("jwt-token", response.getBody().getData().token());
57+
}
58+
}

src/test/java/fun/trackmoney/auth/service/AuthServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void shouldLoginSuccessfullyWhenCredentialsAreValid() {
7171
LoginResponseDTO response = authService.login(loginDto);
7272

7373
// Assert
74-
assertEquals(expectedToken, response.Token());
74+
assertEquals(expectedToken, response.token());
7575
}
7676

7777
@Test

0 commit comments

Comments
 (0)