0% found this document useful (0 votes)
34 views4 pages

User Service Unit Tests in Java

servicio test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

User Service Unit Tests in Java

servicio test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

package com.adrian.reservapp.

model;

import static org.mockito.ArgumentMatchers.any;


import static org.mockito.Mockito.when;

import java.sql.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import com.adrian.reservapp.model.dto.UserDTO;
import com.adrian.reservapp.model.dto.UserFullResponseDTO;
import com.adrian.reservapp.model.entities.UsersEntity;
import com.adrian.reservapp.model.mapper.Mapeador;
import com.adrian.reservapp.model.repositories.UsersRepository;
import com.adrian.reservapp.model.serviceImpl.UsersServiceImpl;
import com.adrian.reservapp.model.test.utils.CommonLibTestUtils;
import com.adrian.reservapp.model.utils.CommonLibUtils;
import com.adrian.reservapp.model.utils.ConstantsUtils;

@ExtendWith(MockitoExtension.class)
public class UserServiceUnitTest {

@Mock
private UsersRepository repo;

@InjectMocks
private UsersServiceImpl usersService;

private Mapeador mapper = Mappers.getMapper(Mapeador.class);

@Test
public void recuperarAllUsers() throws Exception {
List<UsersEntity> listaUsuarios = listaUsuarios();
when(repo.findAll()).thenReturn(listaUsuarios);

List<UserDTO> usuarios = usersService.listAll();

for (int i = 0; i < usuarios.size(); i++) {


CommonLibTestUtils.comprobarUserDTOUserEntity(usuarios.get(i),
listaUsuarios.get(i));
}
}

@Test
public void recuperarUsuarioId() {
UsersEntity usuario = CommonLibUtils.crearUsuarioEntity(23, "ruben",
"perro15", Date.valueOf("2024-08-03"),"ruben","perez","lopez","2000-04-
04","[email protected]","123123123");

when(repo.findById(usuario.getIdusers())).thenReturn(Optional.of(usuario));
Optional<UserDTO> usuarioOpcional =
usersService.obtenerUsuario(usuario.getIdusers());
UserDTO usuarioExistente = usuarioOpcional.get();
CommonLibTestUtils.comprobarUserDTOUserEntity(usuarioExistente,
usuario);
}

@Test
public void altaUsuario() throws Exception {
UsersEntity usuario = CommonLibUtils.crearUsuarioEntity(23, "ruben",
"perro15", Date.valueOf("2024-08-03"),"ruben","perez","lopez","2000-04-
04","[email protected]","123123123");

UserDTO nuevoUsuario = mapper.userEntity2UserDTO(usuario);

when(repo.save(any(UsersEntity.class))).thenReturn(usuario);

when(repo.findByUsername(any(String.class))).thenReturn(Optional.empty());

UserFullResponseDTO usuarioGuardado =
usersService.altaUsuario(nuevoUsuario);

CommonLibTestUtils.comprobarUserFullResponseDTOUserEntity(usuarioGuardado,
usuario);
}

@Test
public void altaUsuario_ThrowsException() {
UsersEntity existingUser = CommonLibUtils.crearUsuarioEntity(23,
"ruben", "perro15", Date.valueOf("2024-08-03"),"ruben","perez","lopez","2000-04-
04","[email protected]","123123123");

UserDTO nuevoUsuario = mapper.userEntity2UserDTO(existingUser);

when(repo.findByUsername(existingUser.getUsername())).thenReturn(Optional.of(existi
ngUser));

Exception exception = Assertions.assertThrows(Exception.class, () -> {


usersService.altaUsuario(nuevoUsuario);
});

String expectedMessage = ConstantsUtils.USER_ALREADY_EXISTS;


String actualMessage = exception.getMessage();

Assertions.assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void altaUsuario_ThrowsExceptionUsernameNull() {
UserDTO nuevoUsuario = CommonLibUtils.crearUsuario(null, "perro15",
Date.valueOf("2024-08-03"));

Exception exception = Assertions.assertThrows(Exception.class, () -> {


usersService.altaUsuario(nuevoUsuario);
});

String expectedMessage = ConstantsUtils.USERNAME_CANNOT_BE_NULL;


String actualMessage = exception.getMessage();

Assertions.assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void altaUsuario_ThrowsExceptionPasswordNull() {
UserDTO nuevoUsuario = CommonLibUtils.crearUsuario("ruben", null,
Date.valueOf("2024-08-03"));

Exception exception = Assertions.assertThrows(Exception.class, () -> {


usersService.altaUsuario(nuevoUsuario);
});

String expectedMessage = ConstantsUtils.PASSWORD_CANNOT_BE_NULL;


String actualMessage = exception.getMessage();

Assertions.assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void altaUsuario_ThrowsExceptionFechaAltaNull() {
UserDTO nuevoUsuario = CommonLibUtils.crearUsuario("ruben",
"password1", null);

Exception exception = Assertions.assertThrows(Exception.class, () -> {


usersService.altaUsuario(nuevoUsuario);
});

String expectedMessage = ConstantsUtils.FECHA_ALTA_CANNOT_BE_NULL;


String actualMessage = exception.getMessage();

Assertions.assertTrue(actualMessage.contains(expectedMessage));
}

@Test
public void altaUsuario_ThrowsExceptionAllNull() {
UserDTO nuevoUsuario = CommonLibUtils.crearUsuario(null, null, null);

Exception exception = Assertions.assertThrows(Exception.class, () -> {


usersService.altaUsuario(nuevoUsuario);
});

String expectedMessage = ConstantsUtils.USERNAME_CANNOT_BE_NULL; //


salta el error en la primera validacion
String actualMessage = exception.getMessage();

Assertions.assertTrue(actualMessage.contains(expectedMessage));
}

private List<UsersEntity> listaUsuarios() {


return Arrays.asList(CommonLibUtils.crearUsuarioEntity(23, "ruben",
"perro15", Date.valueOf("2024-08-03"),"ruben","perez","lopez","2000-04-
04","[email protected]","123123123"),
CommonLibUtils.crearUsuarioEntity(23, "pedro", "perro15",
Date.valueOf("2024-08-04"),"pedro","rodriguez","garcia","2000-05-
07","[email protected]","123125523"),
CommonLibUtils.crearUsuarioEntity(23, "jose", "perro15",
Date.valueOf("2024-08-05"),"jose","garcia","rodriguez","2000-06-
07","[email protected]","123123663"));
}

You might also like