Skip to content

Commit

Permalink
04/01: write test for RoleService
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Jan 4, 2024
1 parent 45bd457 commit 25a88bc
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/spotify/app/mapper/RoleMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import com.spotify.app.model.Role;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Mapper(componentModel = "spring")
public interface RoleMapper {

RoleMapper INSTANCE = Mappers.getMapper(RoleMapper.class);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/spotify/app/service/RoleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ public class RoleService {


private final RoleRepository roleRepository ;
private final RoleMapper roleMapper ;

public List<RoleDTO> listAll() {
return RoleMapper.INSTANCE.rolesToRolesDTO(roleRepository.findAll());
return roleMapper.rolesToRolesDTO(roleRepository.findAll());
}

public Role findByName(String name) {
Expand Down
97 changes: 97 additions & 0 deletions src/test/java/com/spotify/app/role/RoleServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
package com.spotify.app.role;

import com.spotify.app.dto.RoleDTO;
import com.spotify.app.exception.ResourceNotFoundException;
import com.spotify.app.mapper.RoleMapper;
import com.spotify.app.model.Role;
import com.spotify.app.repository.RoleRepository;
import com.spotify.app.service.RoleService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

@ExtendWith(MockitoExtension.class)
public class RoleServiceTest {
private RoleService underTest;


@Mock
private RoleRepository roleRepository ;

@Mock
private RoleMapper roleMapper;

private Role role1 ;

private Role role2 ;

private RoleDTO roleDTO1;

private RoleDTO roleDTO2;

@BeforeEach
void setUp () {
underTest = new RoleService(roleRepository, roleMapper);

role1 = Role.builder()
.id(1)
.name("ROLE_CUSTOMER")
.build();

role2 = Role.builder()
.id(2)
.name("ROLE_ADMIN")
.build();
roleDTO1 = new RoleDTO(1, "ROLE_CUSTOMER");
roleDTO2 = new RoleDTO(2, "ROLE_ADMIN");

}

@Test
public void canGetAllRole () {
// given
List<Role> roles = List.of(role1, role2);
List<RoleDTO> expect = List.of(roleDTO1, roleDTO2);


Mockito.when(roleRepository.findAll()).thenReturn(roles);
Mockito.when(roleMapper.rolesToRolesDTO(Mockito.anyList())).thenReturn(expect);

// when
var actual = underTest.listAll();

// then
Mockito.verify(roleRepository, Mockito.times(1)).findAll();
Mockito.verify(roleMapper, Mockito.times(1)).rolesToRolesDTO(Mockito.anyList());

Assertions.assertEquals(actual, expect);
}

@Test
public void canGetRoleByName () {
// given
String name = "ROLE_CUSTOMER";
// when
Mockito.when(roleRepository.findByName(Mockito.any())).thenReturn(Optional.of(role1));
// then
var actual = underTest.findByName(name);
Assertions.assertEquals(actual, role1);
}

@Test
public void cannotGetRoleByNameWithEmptyRole () {
// given
String name = "ROLE_CUSTOMER";
Mockito.when(roleRepository.findByName(Mockito.any())).thenReturn(Optional.empty());

// when
// then
Assertions.assertThrows(ResourceNotFoundException.class, () -> {
underTest.findByName(name);
});
Mockito.verify(roleRepository, Mockito.times(1)).findByName(Mockito.any());
}
}

0 comments on commit 25a88bc

Please sign in to comment.