Skip to content

Commit

Permalink
Merge pull request #58 from Tave-13th-Project-Team-4-Fiurinee/feature…
Browse files Browse the repository at this point in the history
…/test

Feature/test 테스트 코드 작성
  • Loading branch information
qormoon authored Jul 13, 2024
2 parents f129922 + fba8451 commit c8097ec
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@ public ResponseEntity<FlowerResponseDTO> getTodayFlower() {
return ResponseEntity.ok(flower);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ public class MatchingFlower {
@JoinColumn(name = "flowerId")
private Flower flower;

public static MatchingFlower createTestInstance() {
return new MatchingFlower();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest
@ActiveProfiles("test")
class FiurineeApplicationTests {

// @Test
// void contextLoads() {
// }
@Test
void contextLoads() {
}

}
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));
}
}
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);
}
}
}
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));
}
}
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);
}
}
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);
}
}
Loading

0 comments on commit c8097ec

Please sign in to comment.