Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
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.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,16 +27,11 @@
import static org.springframework.test.web.client.ExpectedCount.times;

@ExtendWith(MockitoExtension.class)
@SpringBootTest
public class CafeteriaServiceV2Test {

@Autowired
private CafeteriaServiceV2 cafeteriaServiceV2;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
@InjectMocks
private CacheServiceV2 cacheServiceV2;
@MockBean
@Mock
private CafeteriaRepositoryV2 cafeteriaRepositoryV2;

@Test
Expand All @@ -54,40 +51,4 @@ public void checkUserCafeteriaList() {
// then
assertThat(result).usingRecursiveComparison().isEqualTo(resultList);
}

// @Test
// @DisplayName("campusId, cafeteriaName을 키값으로 캐싱하여 cafeteriaId 값을 찾는다.")
// public void checkCacheData() {
// // given
// int campusId = 1;
// String cafeteriaName = "교직원식당";
// int expectedCafeteriaId = 4;
// String expectedCacheKey = "cafeteridId::" + campusId + "::" + cafeteriaName; // 캐싱 키 예상값
//
// // Mock 설정: DB 조회 시 특정 값 반환하도록 설정
// Mockito.when(cafeteriaRepositoryV2.findCafeteriaId(cafeteriaName, campusId)).thenReturn(Optional.of(expectedCafeteriaId));
//
// // when: 첫 번째 호출 (DB 조회 발생 후 Redis에 캐싱됨)
// int cafeteriaId = cafeteriaServiceV2.getCafeteriaId(cafeteriaName, campusId);
//
// // then: DB에서 가져온 값이 기대한 값과 같은지 확인
// assertThat(cafeteriaId).isEqualTo(expectedCafeteriaId);
//
// // Redis에서 직접 키 조회하여 확인
// ValueOperations<String, String> valueOps = redisTemplate.opsForValue();
// String cachedValue = valueOps.get(expectedCacheKey);
//
// assertThat(cachedValue).isNotNull(); // 캐시가 존재해야 함
// assertThat(Integer.parseInt(cachedValue)).isEqualTo(expectedCafeteriaId); // 저장된 값이 예상한 값과 같은지 확인
//
// // 캐시가 적용되었는지 검증: 두 번째 호출에서는 DB 조회가 발생하면 안 됨
// int cachedCafeteriaId = cafeteriaServiceV2.getCafeteriaId(cafeteriaName, campusId);
//
// // 두 번째 호출은 캐시에서 가져와야 하므로 DB 호출이 한 번만 발생해야 함
// verify(cafeteriaRepositoryV2, Mockito.times(1)).findCafeteriaId(cafeteriaName, campusId);
//
// // 캐시된 값과 두 번째 조회한 값이 같은지 확인
// assertThat(cachedCafeteriaId).isEqualTo(expectedCafeteriaId);
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,26 @@

import com.example.Jinus.entity.cafeteria.CampusEntity;
import com.example.Jinus.repository.v2.cafeteria.CampusRepositoryV2;
import com.example.Jinus.repository.v2.userInfo.UserRepositoryV2;
import com.example.Jinus.service.v2.cafeteria.CampusServiceV2;
import com.example.Jinus.service.v2.userInfo.UserServiceV2;
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.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@SpringBootTest
public class CampusServiceV2Test {

@Autowired
@InjectMocks
private CampusServiceV2 campusServiceV2;
@MockBean
@Mock
private CampusRepositoryV2 campusRepositoryV2;

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.example.Jinus.service.notice;

import com.example.Jinus.entity.notice.NoticeCategoryEntity;
import com.example.Jinus.repository.v2.notice.CategoryRepositoryV2;
import com.example.Jinus.service.v2.notice.CategoryServiceV2;
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.assertEquals;
import static org.mockito.Mockito.*;


@ExtendWith(MockitoExtension.class)
public class CategoryServiceV2Test {

@Mock
private CategoryRepositoryV2 noticeCategoryRepository;
@InjectMocks
private CategoryServiceV2 categoryService;

@Test
@DisplayName("사용자 학과id와 일치하는 카테고리 찾기")
void checkGetCategoryEntityTest() {
// Given - 테스트 데이터 생성
int departmentId = 1;
List<NoticeCategoryEntity> expectedCategories = List.of(
new NoticeCategoryEntity(1, 1, "공지사항", 0, 0, 0, null),
new NoticeCategoryEntity(2, 1, "학사일정", 0, 0, 0, null)
);

// Mocking - 목 객체의 동작 정의
when(noticeCategoryRepository.findCategoryListByDepartmentId(departmentId))
.thenReturn(expectedCategories);

// When - 서비스 호출
List<NoticeCategoryEntity> result = categoryService.getCategoryEntity(departmentId);

// Then - 결과 검증
assertEquals(expectedCategories.size(), result.size()); // 2개의 카테고리 반환해야 함
assertEquals(expectedCategories, result);
// 서비스 메소드 중복 호출 방지를 위한 호출 횟수 확인
verify(noticeCategoryRepository, times(1))
.findCategoryListByDepartmentId(departmentId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.example.Jinus.service.notice;

import com.example.Jinus.dto.response.ListItemDto;
import com.example.Jinus.entity.notice.NoticeCategoryEntity;
import com.example.Jinus.entity.notice.NoticeEntity;
import com.example.Jinus.repository.v2.notice.NoticeRepositoryV2;
import com.example.Jinus.service.v2.notice.CategoryServiceV2;
import com.example.Jinus.service.v2.notice.NoticeServiceV2;
import org.junit.jupiter.api.BeforeEach;
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.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
public class NoticeServiceV2Test {

@InjectMocks
private NoticeServiceV2 noticeServiceV2;
@Mock
private CategoryServiceV2 categoryServiceV2;
@Mock
private NoticeRepositoryV2 noticeRepositoryV2;

private List<NoticeCategoryEntity> mockCategoryEntities;
private List<NoticeEntity> mockNoticeEntities;

@BeforeEach
void setUp() {
// 공지 카테고리 엔티티 Mock 데이터 생성
mockCategoryEntities = new ArrayList<>();
NoticeCategoryEntity category = new NoticeCategoryEntity();
category.setId(1);
category.setMi(123);
category.setBbsId(456);
category.setCategory("학과공지");
mockCategoryEntities.add(category);

// 공지 엔티티 Mock 데이터 생성
mockNoticeEntities = new ArrayList<>();
NoticeEntity notice = new NoticeEntity();
notice.setTitle("시험 일정 공지");
notice.setCreatedAt("2025-03-24");
notice.setNttSn(1001);
mockNoticeEntities.add(notice);
}

@Test
@DisplayName("학과정보 존재하는 경우 공지리스트 반환")
void checkExistUserReturnNotice() {
// given
String departmentEng = "computer";
int departmentId = 1;

when(categoryServiceV2.getCategoryEntity(departmentId)).thenReturn(mockCategoryEntities);
when(noticeRepositoryV2.findNoticeListByCategoryId(1)).thenReturn(mockNoticeEntities);

// when
String result = noticeServiceV2.existUserReturnNotice(departmentEng, departmentId);

// then
assertNotNull(result);
assertTrue(result.contains("학과공지")); // JSON 결과에 "학과공지" 포함 확인
verify(categoryServiceV2, times(1)).getCategoryEntity(departmentId);
}

@Test
@DisplayName("공지가 존재하지 않는 경우")
void checkThereIsNoNoticeData() {
// given
String departmentEng = "computer";
int departmentId = 1;

when(categoryServiceV2.getCategoryEntity(departmentId)).thenReturn(new ArrayList<>());

// when
String result = noticeServiceV2.existUserReturnNotice(departmentEng, departmentId);

// then
assertNotNull(result);
assertTrue(result.contains("최근에 등록된 공지사항이 없어!"));
}

@Test
@DisplayName("학과 정보가 없어서 학과 등록 블록 반환")
void checkDoesNotExistUserReturnBlock() {
// when
String result = noticeServiceV2.doesNotExistUserReturnBlock();

// then
assertNotNull(result);
assertTrue(result.contains("학과 등록"));
assertTrue(result.contains("66cf0c8ae5715f75b254dfea")); // 블록 ID 포함 여부 확인
}

@Test
@DisplayName("공지 가져오기")
void checkGetNoticeList() {
// given
int categoryId = 1;
String departmentEng = "computer";
String mi = "123";
String bbsId = "456";

when(noticeRepositoryV2.findNoticeListByCategoryId(categoryId)).thenReturn(mockNoticeEntities);

// when
List<ListItemDto> result = noticeServiceV2.getNoticeList(categoryId, mi, bbsId, departmentEng);

// then
assertNotNull(result);
assertEquals(1, result.size());
assertEquals("시험 일정 공지", result.getFirst().getTitle());
}

@Test
@DisplayName("url 생성 테스트")
void checkNoticeDetailUrl() {
// given
String departmentEng = "computer";
String mi = "123";
String bbsId = "456";
int nttSn = 1001;

// when
String url = noticeServiceV2.noticeDetailUrl(departmentEng, mi, bbsId, nttSn);

// then
assertEquals("https://www.gnu.ac.kr/computer/na/ntt/selectNttInfo.do?mi=123&bbsId=456&nttSn=1001", url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
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.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
@SpringBootTest
public class DepartmentServiceV2Test {
@Autowired
@InjectMocks
private DepartmentServiceV2 departmentServiceV2;
@MockBean
@Mock
private DepartmentRepositoryV2 departmentRepositoryV2;

@Test
Expand Down
Loading