-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
275 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,4 @@ void testDeleteNonExistingOption() throws Exception{ | |
.andExpect(status().isNotFound()); | ||
} | ||
|
||
|
||
|
||
} |
90 changes: 90 additions & 0 deletions
90
application/src/test/java/com/terte/service/menu/ChoiceServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.terte.service.menu; | ||
import com.terte.common.exception.NotFoundException; | ||
import com.terte.entity.menu.Choice; | ||
import com.terte.repository.menu.ChoiceRepository; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
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.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ChoiceServiceTest { | ||
@Mock | ||
ChoiceRepository choiceRepository; | ||
|
||
@InjectMocks | ||
ChoiceServiceImpl choiceService; | ||
|
||
@Test | ||
@DisplayName("선택지 생성") | ||
void createChoice() { | ||
Choice choice = new Choice(null, "샷 추가", 500); | ||
Choice createdChoice = new Choice(1L, "샷 추가", 500); | ||
when(choiceRepository.save(choice)).thenReturn(createdChoice); | ||
|
||
Choice result = choiceService.createChoice(choice); | ||
|
||
assertNotNull(result.getId()); | ||
assertEquals(choice.getName(), result.getName()); | ||
assertEquals(choice.getPrice(), result.getPrice()); | ||
|
||
verify(choiceRepository, times(1)).save(choice); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하는 선택지 수정") | ||
void updateChoice() { | ||
Choice choice = new Choice(1L, "샷 추가", 500); | ||
Choice existingChoice = new Choice(1L, "샷 추가", 500); | ||
when(choiceRepository.findById(choice.getId())).thenReturn(existingChoice); | ||
when(choiceRepository.save(choice)).thenReturn(choice); | ||
|
||
Choice result = choiceService.updateChoice(choice); | ||
|
||
assertEquals(choice.getId(), result.getId()); | ||
assertEquals(choice.getName(), result.getName()); | ||
assertEquals(choice.getPrice(), result.getPrice()); | ||
|
||
verify(choiceRepository, times(1)).findById(choice.getId()); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 선택지 수정") | ||
void updateChoiceWithNonExistingChoice() { | ||
Choice choice = new Choice(1L, "샷 추가", 500); | ||
when(choiceRepository.findById(choice.getId())).thenReturn(null); | ||
|
||
assertThrows(NotFoundException.class, () -> choiceService.updateChoice(choice)); | ||
verify(choiceRepository, times(1)).findById(choice.getId()); | ||
} | ||
|
||
@Test | ||
@DisplayName("선택지 삭제") | ||
void deleteChoice() { | ||
Long choiceId = 1L; | ||
Choice existingChoice = new Choice(choiceId, "샷 추가", 500); | ||
when(choiceRepository | ||
.findById(choiceId)) | ||
.thenReturn(existingChoice); | ||
|
||
choiceService.deleteChoice(choiceId); | ||
verify(choiceRepository, times(1)).deleteById(choiceId); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 선택지 삭제") | ||
void deleteChoiceWithNonExistingChoice() { | ||
Long choiceId = 1L; | ||
when(choiceRepository | ||
.findById(choiceId)) | ||
.thenReturn(null); | ||
assertThrows(NotFoundException.class, () -> choiceService.deleteChoice(choiceId)); | ||
verify(choiceRepository, never()).deleteById(choiceId); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
application/src/test/java/com/terte/service/menu/OptionServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.terte.service.menu; | ||
|
||
import com.terte.common.exception.NotFoundException; | ||
import com.terte.entity.menu.Choice; | ||
import com.terte.entity.menu.Option; | ||
import com.terte.repository.menu.OptionRepository; | ||
import org.junit.jupiter.api.DisplayName; | ||
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 java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class OptionServiceTest { | ||
@Mock | ||
OptionRepository optionRepository; | ||
|
||
@InjectMocks | ||
OptionServiceImpl optionService; | ||
|
||
@Test | ||
@DisplayName("옵션 생성") | ||
void createOption() { | ||
Option option = new Option(null, "샷 추가", true, false, null); | ||
Option createdOption = new Option(1L, "샷 추가", true, false, null); | ||
when(optionRepository.save(option)).thenReturn(createdOption); | ||
|
||
Option result = optionService.createOption(option); | ||
|
||
assertNotNull(result.getId()); | ||
assertEquals(option.getName(), result.getName()); | ||
assertEquals(option.getRequired(), result.getRequired()); | ||
assertEquals(option.getMultipleSelection(), result.getMultipleSelection()); | ||
|
||
verify(optionRepository, times(1)).save(option); | ||
|
||
} | ||
|
||
@Test | ||
@DisplayName("존재하는 옵션 수정") | ||
void updateOption() { | ||
Option option = new Option(1L, "샷 추가", true, false, null); | ||
Option existingOption = new Option(1L, "샷 추가", true, false, null); | ||
when(optionRepository.findById(option.getId())).thenReturn(existingOption); | ||
when(optionRepository.save(option)).thenReturn(option); | ||
|
||
Option result = optionService.updateOption(option); | ||
|
||
assertEquals(option.getId(), result.getId()); | ||
assertEquals(option.getName(), result.getName()); | ||
assertEquals(option.getRequired(), result.getRequired()); | ||
assertEquals(option.getMultipleSelection(), result.getMultipleSelection()); | ||
|
||
verify(optionRepository, times(1)).findById(option.getId()); | ||
verify(optionRepository, times(1)).save(option); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 옵션 수정") | ||
void updateOptionNotFound() { | ||
Option option = new Option(1L, "샷 추가", true, false, null); | ||
when(optionRepository.findById(option.getId())).thenReturn(null); | ||
|
||
|
||
assertThrows(NotFoundException.class, () -> optionService.updateOption(option)); | ||
verify(optionRepository, times(1)).findById(option.getId()); | ||
verify(optionRepository, never()).save(option); | ||
} | ||
|
||
@Test | ||
@DisplayName("옵션 삭제") | ||
void deleteOption() { | ||
Long optionId = 1L; | ||
Option option = new Option(optionId, "샷 추가", true, false, null); | ||
when(optionRepository.findById(optionId)).thenReturn(option); | ||
|
||
optionService.deleteOption(optionId); | ||
|
||
verify(optionRepository, times(1)).deleteById(optionId); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 옵션 삭제") | ||
void deleteOptionNotFound() { | ||
Long optionId = 1L; | ||
when(optionRepository.findById(optionId)). | ||
thenReturn(null); | ||
|
||
assertThrows(NotFoundException.class, () -> optionService.deleteOption(optionId)); | ||
verify(optionRepository, never()).deleteById(optionId); | ||
} | ||
|
||
@Test | ||
@DisplayName("옵션의 선택지 조회") | ||
void getChoicesById() { | ||
Choice choice = new Choice(1L, "샷 추가", 500); | ||
Option option = new Option(1L, "샷 추가", true, false, List.of(choice)); | ||
when(optionRepository.findById(option.getId())).thenReturn(option); | ||
|
||
List<Choice> result = optionService.getChoicesById(option.getId()); | ||
assertEquals(option.getChoices(), result); | ||
verify(optionRepository, times(1)).findById(option.getId()); | ||
} | ||
|
||
@Test | ||
@DisplayName("존재하지 않는 옵션의 선택지 조회") | ||
void getChoicesByIdNotFound() { | ||
Long optionId = 1L; | ||
when(optionRepository.findById(optionId)). | ||
thenReturn(null); | ||
|
||
assertThrows(NotFoundException.class, () -> optionService.getChoicesById(optionId)); | ||
verify(optionRepository, times(1)).findById(optionId); | ||
|
||
} | ||
|
||
} |