Skip to content

Commit

Permalink
test(backend): UserRepository invocation and UserService register
Browse files Browse the repository at this point in the history
  • Loading branch information
markkovari committed Jul 9, 2024
1 parent 21990d9 commit 2b2113b
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 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;

import static org.junit.jupiter.api.Assertions.assertTrue;

Check warning on line 11 in backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java

View workflow job for this annotation

GitHub Actions / build

[testtool] reported by reviewdog 🐶 Import statement for 'org.junit.jupiter.api.Assertions.assertTrue' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. Raw Output: /github/workspace/./backend/src/test/java/com/greenfoxacademy/backend/controller/UserRegistrationTest.java:11:1: warning: Import statement for 'org.junit.jupiter.api.Assertions.assertTrue' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)

/**
* 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 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;

import static org.mockito.Mockito.times;

Check warning on line 12 in backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java

View workflow job for this annotation

GitHub Actions / build

[testtool] reported by reviewdog 🐶 Import statement for 'org.mockito.Mockito.times' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. Raw Output: /github/workspace/./backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java:12:1: warning: Import statement for 'org.mockito.Mockito.times' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)
import static org.mockito.Mockito.verify;

Check warning on line 13 in backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java

View workflow job for this annotation

GitHub Actions / build

[testtool] reported by reviewdog 🐶 Import statement for 'org.mockito.Mockito.verify' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. Raw Output: /github/workspace/./backend/src/test/java/com/greenfoxacademy/backend/controller/UserServiceTest.java:13:1: warning: Import statement for 'org.mockito.Mockito.verify' is in the wrong order. Should be in the 'STATIC' group, expecting not assigned imports on this line. (com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck)

/**
* 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 2b2113b

Please sign in to comment.