-
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.
Merge pull request #58 from Tave-13th-Project-Team-4-Fiurinee/feature…
…/test Feature/test 테스트 코드 작성
- Loading branch information
Showing
13 changed files
with
734 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,4 @@ public ResponseEntity<FlowerResponseDTO> getTodayFlower() { | |
return ResponseEntity.ok(flower); | ||
} | ||
|
||
|
||
|
||
|
||
} |
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
72 changes: 72 additions & 0 deletions
72
src/test/java/com/example/fiurinee/integration/alarm/AlarmServiceTest.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,72 @@ | ||
package com.example.fiurinee.integration.alarm; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
|
||
import com.example.fiurinee.domain.alarm.dto.AlarmResponseDTO; | ||
import com.example.fiurinee.domain.alarm.service.AlarmService; | ||
import com.example.fiurinee.domain.member.entity.Member; | ||
import com.example.fiurinee.domain.member.entity.Role; | ||
import com.example.fiurinee.domain.member.repository.MemberRepository; | ||
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 static org.mockito.Mockito.*; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class AlarmServiceTest { | ||
@Mock | ||
private MemberRepository memberRepository; | ||
|
||
@InjectMocks | ||
private AlarmService alarmService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void updateAlarmStatus_memberExists_updatesStatus() { | ||
Long memberId = 1L; | ||
boolean newAlarmStatus = true; | ||
|
||
Member member = Member.builder() | ||
.email("[email protected]") | ||
.name("백지현") | ||
.socialId("12345") | ||
.role(Role.USER) | ||
.kakaoAccessToken("token") | ||
.profileImage(1) | ||
.alarm(false) | ||
.build(); | ||
|
||
when(memberRepository.findById(memberId)).thenReturn(Optional.of(member)); | ||
when(memberRepository.save(any(Member.class))).thenAnswer(invocation -> invocation.getArgument(0)); | ||
|
||
AlarmResponseDTO result = alarmService.updateAlarmStatus(memberId, newAlarmStatus); | ||
|
||
assertNotNull(result); | ||
assertTrue(result.isAlarm()); | ||
assertEquals(newAlarmStatus, member.isAlarm()); | ||
verify(memberRepository, times(1)).findById(memberId); | ||
verify(memberRepository, times(1)).save(member); | ||
} | ||
|
||
@Test | ||
void updateAlarmStatus_memberDoesNotExist_throwsException() { | ||
Long memberId = 1L; | ||
boolean newAlarmStatus = true; | ||
|
||
when(memberRepository.findById(memberId)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(IllegalArgumentException.class, () -> alarmService.updateAlarmStatus(memberId, newAlarmStatus)); | ||
verify(memberRepository, times(1)).findById(memberId); | ||
verify(memberRepository, never()).save(any(Member.class)); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/com/example/fiurinee/integration/flower/FlowerServiceTest.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,75 @@ | ||
package com.example.fiurinee.integration.flower; | ||
|
||
import com.example.fiurinee.domain.flower.dto.FlowerResponseDTO; | ||
import com.example.fiurinee.domain.flower.entity.Flower; | ||
import com.example.fiurinee.domain.flower.repository.FlowerRepository; | ||
import com.example.fiurinee.domain.flower.service.FlowerService; | ||
import com.example.fiurinee.global.exception.CustomException; | ||
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 java.net.URL; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class FlowerServiceTest { | ||
@Mock | ||
private FlowerRepository flowerRepository; | ||
|
||
@InjectMocks | ||
private FlowerService flowerService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
|
||
@Test | ||
void testFindByNameAndFlowerLanguage() { | ||
String name = "장미"; | ||
String flowerLanguage = "사랑"; | ||
Flower flower = Flower.builder() | ||
.name(name) | ||
.period(601L) | ||
.flowerLanguage(flowerLanguage) | ||
.explain("꽃이 아름다워요!") | ||
.image(toURL("https://example.com/rose.jpg")) | ||
.build(); | ||
|
||
when(flowerRepository.findByNameAndFlowerLanguage(name, flowerLanguage)).thenReturn(Optional.of(flower)); | ||
|
||
Flower foundFlower = flowerService.findByNameAndFlowerLanguage(name, flowerLanguage); | ||
|
||
assertThat(foundFlower).isNotNull(); | ||
assertThat(foundFlower.getName()).isEqualTo(name); | ||
} | ||
|
||
@Test | ||
void testFindByNameAndFlowerLanguageThrowsException() { | ||
String name = "Unknown"; | ||
String flowerLanguage = "Unknown"; | ||
|
||
when(flowerRepository.findByNameAndFlowerLanguage(name, flowerLanguage)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(CustomException.class, () -> { | ||
flowerService.findByNameAndFlowerLanguage(name, flowerLanguage); | ||
}); | ||
} | ||
|
||
private URL toURL(String urlString) { | ||
try { | ||
return new URL(urlString); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/test/java/com/example/fiurinee/integration/inputMessage/InputMessageServiceTest.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,72 @@ | ||
package com.example.fiurinee.integration.inputMessage; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.example.fiurinee.domain.inputMessage.entity.InputMessage; | ||
import com.example.fiurinee.domain.inputMessage.repository.InputMessageRepository; | ||
import com.example.fiurinee.domain.inputMessage.service.InputMessageService; | ||
import com.example.fiurinee.domain.member.entity.Member; | ||
import com.example.fiurinee.domain.member.entity.Role; | ||
import com.example.fiurinee.domain.member.repository.MemberRepository; | ||
import com.example.fiurinee.global.exception.CustomException; | ||
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 java.util.Optional; | ||
|
||
public class InputMessageServiceTest { | ||
@Mock | ||
private MemberRepository memberRepository; | ||
|
||
@Mock | ||
private InputMessageRepository inputMessageRepository; | ||
|
||
@InjectMocks | ||
private InputMessageService inputMessageService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void saveInputMessage_validMember_savesMessage() { | ||
Long memberId = 1L; | ||
String message = "메롱 메롱 "; | ||
|
||
Member member = Member.builder() | ||
.email("[email protected]") | ||
.name("백지현") | ||
.socialId("12345") | ||
.role(Role.USER) | ||
.kakaoAccessToken("token") | ||
.profileImage(1) | ||
.alarm(false) | ||
.build(); | ||
|
||
when(memberRepository.findById(memberId)).thenReturn(Optional.of(member)); | ||
when(inputMessageRepository.save(any(InputMessage.class))).thenAnswer(invocation -> invocation.getArgument(0)); | ||
|
||
boolean result = inputMessageService.saveInputMessage(memberId, message); | ||
|
||
assertTrue(result); | ||
verify(memberRepository, times(1)).findById(memberId); | ||
verify(inputMessageRepository, times(1)).save(any(InputMessage.class)); | ||
} | ||
|
||
@Test | ||
void saveInputMessage_invalidMember_throwsException() { | ||
Long memberId = 1L; | ||
String message = "메롱 메롱 "; | ||
|
||
when(memberRepository.findById(memberId)).thenReturn(Optional.empty()); | ||
|
||
assertThrows(CustomException.class, () -> inputMessageService.saveInputMessage(memberId, message)); | ||
verify(memberRepository, times(1)).findById(memberId); | ||
verify(inputMessageRepository, never()).save(any(InputMessage.class)); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/test/java/com/example/fiurinee/integration/mail/MailServiceTest.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,86 @@ | ||
package com.example.fiurinee.integration.mail; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Map; | ||
|
||
import com.example.fiurinee.domain.mail.MailService; | ||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
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.mail.javamail.JavaMailSender; | ||
import org.thymeleaf.context.Context; | ||
|
||
import com.example.fiurinee.domain.member.entity.Member; | ||
import com.example.fiurinee.domain.anniversary.entity.Anniversary; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
public class MailServiceTest { | ||
@Mock | ||
private JavaMailSender mailSender; | ||
|
||
@Mock | ||
private SpringTemplateEngine templateEngine; | ||
|
||
@InjectMocks | ||
private MailService mailService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void sendEmail_validData_sendsEmail() throws MessagingException { | ||
MimeMessage mimeMessage = mock(MimeMessage.class); | ||
when(mailSender.createMimeMessage()).thenReturn(mimeMessage); | ||
when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); | ||
|
||
mailService.sendEmail("[email protected]", "Subject", "templateName", Map.of("key", "value")); | ||
|
||
verify(mailSender, times(1)).send(mimeMessage); | ||
} | ||
|
||
@Test | ||
void sendAnniversaryEmail_validData_sendsEmail() throws MessagingException { | ||
Member member = Member.builder() | ||
.email("[email protected]") | ||
.name("백지현") | ||
.build(); | ||
Anniversary anniversary = Anniversary.builder() | ||
.name("누구 기념일") | ||
.build(); | ||
|
||
MimeMessage mimeMessage = mock(MimeMessage.class); | ||
when(mailSender.createMimeMessage()).thenReturn(mimeMessage); | ||
when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); | ||
|
||
mailService.sendAnniversaryEmail(member, anniversary); | ||
|
||
verify(mailSender, times(1)).send(mimeMessage); | ||
} | ||
|
||
@Test | ||
void sendPreAnniversaryEmail_validData_sendsEmail() throws MessagingException { | ||
Member member = Member.builder() | ||
.email("[email protected]") | ||
.name("백지현") | ||
.build(); | ||
Anniversary anniversary = Anniversary.builder() | ||
.name("누구누구 기념일") | ||
.build(); | ||
|
||
MimeMessage mimeMessage = mock(MimeMessage.class); | ||
when(mailSender.createMimeMessage()).thenReturn(mimeMessage); | ||
when(templateEngine.process(any(String.class), any(Context.class))).thenReturn("HTML content"); | ||
|
||
mailService.sendPreAnniversaryEmail(member, anniversary); | ||
|
||
verify(mailSender, times(1)).send(mimeMessage); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/test/java/com/example/fiurinee/integration/matchingFlower/MatchingFlowerServiceTest.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,34 @@ | ||
package com.example.fiurinee.integration.matchingFlower; | ||
|
||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.times; | ||
|
||
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 com.example.fiurinee.domain.matchingFlower.entity.MatchingFlower; | ||
import com.example.fiurinee.domain.matchingFlower.repository.MatchingFlowerRepository; | ||
import com.example.fiurinee.domain.matchingFlower.service.MatchingFlowerService; | ||
|
||
public class MatchingFlowerServiceTest { | ||
@Mock | ||
private MatchingFlowerRepository matchingFlowerRepository; | ||
|
||
@InjectMocks | ||
private MatchingFlowerService matchingFlowerService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testSave() { | ||
MatchingFlower matchingFlower = MatchingFlower.createTestInstance(); | ||
matchingFlowerService.save(matchingFlower); | ||
verify(matchingFlowerRepository, times(1)).save(matchingFlower); | ||
} | ||
} |
Oops, something went wrong.