Skip to content

Commit

Permalink
Feat: modified SportMapper to static method
Browse files Browse the repository at this point in the history
  • Loading branch information
kz44 committed Feb 1, 2024
1 parent e49d34e commit 6067e32
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@

import com.sportsmatch.dtos.SportDTO;
import com.sportsmatch.models.Sport;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Component;

@Getter
@Setter
@RequiredArgsConstructor
@Component

public class SportMapper {

/**
Expand All @@ -19,7 +12,7 @@ public class SportMapper {
* @param entity The Sport entity to be converted.
* @return SportDTO containing information from the Sport entity.
*/
public SportDTO toDTO(Sport entity) {
public static SportDTO toDTO(Sport entity) {
return SportDTO.builder()
.name(entity.getName())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Sport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class SportServiceImp implements SportService {

private final SportRepository sportRepository;
private final SportMapper sportMapper;


/**
Expand All @@ -27,7 +26,7 @@ public class SportServiceImp implements SportService {
public List<SportDTO> getAllSports(final Pageable pageable) {
return sportRepository.findAll(pageable)
.stream()
.map(sportMapper::toDTO)
.map(SportMapper::toDTO)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import com.sportsmatch.repositories.SportRepository;
import com.sportsmatch.services.SportServiceImp;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
Expand All @@ -17,13 +18,16 @@
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

@SpringBootTest
@ExtendWith(MockitoExtension.class)
class SportServiceImpTest {

@Mock
private SportRepository sportRepository;


@InjectMocks
private SportServiceImp sportService;

Expand All @@ -44,7 +48,8 @@ void getAllSportsShouldReturnAllSportsWhenRequired() {
List<SportDTO> expectedSportDTOs = Arrays.asList(sportDTO1, sportDTO2);

// Mocking repository
Mockito.when(sportRepository.findAll(Mockito.any(Pageable.class))).thenReturn(sportsPage);
when(sportRepository.findAll(any(Pageable.class))).thenReturn(sportsPage);


// Act
List<SportDTO> result = sportService.getAllSports(pageable);
Expand Down

0 comments on commit 6067e32

Please sign in to comment.