Skip to content

Commit

Permalink
test(backend): UserRepository invocation and UserService register
Browse files Browse the repository at this point in the history
Signed-off-by: Márk Kővári <kovarimarkofficial@gmail.com>
  • Loading branch information
markkovari committed Jul 9, 2024
1 parent 21990d9 commit 4976652
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
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);
}
}
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);

}

}

0 comments on commit 4976652

Please sign in to comment.