-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(backend): UserRepository invocation and UserService register
Signed-off-by: Márk Kővári <kovarimarkofficial@gmail.com>
- Loading branch information
1 parent
21990d9
commit 4976652
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.greenfoxacademy.backend.controller; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.greenfoxacademy.backend.dtos.RegisterUserDto; | ||
import com.greenfoxacademy.backend.repositories.UserRepository; | ||
import com.greenfoxacademy.backend.services.UserService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
/** | ||
* This class runs a test that verifies if a user is successfully registered in the database. | ||
*/ | ||
@SpringBootTest | ||
@AutoConfigureTestDatabase | ||
public class UserRegistrationTest { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Test | ||
public void userIsSuccessfulRegisteredInDatabase() { | ||
RegisterUserDto newUser = new RegisterUserDto(); | ||
newUser.setFirstName("John"); | ||
newUser.setLastName("Doe"); | ||
newUser.setEmail("john.doe@example.com"); | ||
newUser.setPassword("password"); | ||
|
||
userService.register(newUser); | ||
|
||
boolean isRegistered = userRepository.existsByEmail("john.doe@example.com"); | ||
assertTrue(isRegistered); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.greenfoxacademy.backend.controller; | ||
|
||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import com.greenfoxacademy.backend.dtos.RegisterUserDto; | ||
import com.greenfoxacademy.backend.repositories.UserRepository; | ||
import com.greenfoxacademy.backend.services.UserService; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
/** | ||
* This class runs a test to verify if the register method in the userService is properly called. | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public class UserServiceTest { | ||
|
||
@Mock | ||
private UserService userService; | ||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@InjectMocks | ||
private UserController userController; | ||
|
||
@Test | ||
public void registerMethodIsSuccessfullyCalled() { | ||
RegisterUserDto registerUserDto = new RegisterUserDto(); | ||
registerUserDto.setFirstName("John"); | ||
registerUserDto.setLastName("Doe"); | ||
registerUserDto.setEmail("john.doe@gmail.com"); | ||
registerUserDto.setPassword("password"); | ||
|
||
userService.register(registerUserDto); | ||
|
||
verify(userService, times(1)).register(registerUserDto); | ||
|
||
} | ||
|
||
} |