diff --git a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java new file mode 100644 index 00000000..bb9d36d2 --- /dev/null +++ b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java @@ -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); + } +} diff --git a/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java new file mode 100644 index 00000000..ebc074e9 --- /dev/null +++ b/backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java @@ -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); + + } + +}