From 30c9d268cac6111e72905fea9fe1133d06efd72e Mon Sep 17 00:00:00 2001 From: Subi Hwang Date: Wed, 7 Feb 2024 18:06:38 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20pattern=20test=20code=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20#41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oot/custom/pattern/entity/Pattern.java | 1 + .../oot/pattern/PatternServiceTest.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java diff --git a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java index 1a19186..f4f0a32 100644 --- a/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java +++ b/src/main/java/com/endlesshorses/oot/custom/pattern/entity/Pattern.java @@ -26,4 +26,5 @@ public class Pattern { @OneToMany(mappedBy = "pattern") private List results = new ArrayList<>(); + } diff --git a/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java b/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java new file mode 100644 index 0000000..6ec0962 --- /dev/null +++ b/src/test/java/com/endlesshorses/oot/pattern/PatternServiceTest.java @@ -0,0 +1,58 @@ +package com.endlesshorses.oot.pattern; + +import com.endlesshorses.oot.custom.pattern.dto.PatternListResponseDto; +import com.endlesshorses.oot.custom.pattern.entity.Pattern; +import com.endlesshorses.oot.custom.pattern.repository.PatternRepository; +import com.endlesshorses.oot.custom.pattern.service.PatternService; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Arrays; +import java.util.List; + +import static org.mockito.Mockito.when; + +@SpringBootTest +public class PatternServiceTest { + @Mock + private PatternRepository patternRepository; + + @InjectMocks + private PatternService patternService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + public void testList() { + + Pattern pattern1 = new Pattern(); + Pattern pattern2 = new Pattern(); + + + List patternList = Arrays.asList(pattern1, pattern2); + + //패턴 레포지토리의 findAll() 메서드가 호출될 때 패턴 엔티티 목록 반환하도록 설정 + when(patternRepository.findAll()).thenReturn(patternList); + + // 패턴 서비스의 list() 메서드 호출 + List result = patternService.list(); + + // 결과 + if (!result.isEmpty()) { + Assertions.assertEquals("리브패턴", result.get(0).getName()); + Assertions.assertEquals("리브러그패턴", result.get(1).getName()); + } else { + // 패턴 목록이 비어있는 경우에 대한 예외 처리 + Assertions.fail("패턴 목록이 비어있습니다."); + } + } + +}