Skip to content

Commit 137141e

Browse files
author
EpicFn
committed
fix : test 시 mock 빈 사용
1 parent f074ed7 commit 137141e

File tree

8 files changed

+84
-0
lines changed

8 files changed

+84
-0
lines changed

src/test/java/org/tuna/zoopzoop/backend/domain/dashboard/controller/DashboardControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.commons.codec.binary.Hex;
44
import org.junit.jupiter.api.*;
5+
import org.mockito.Mockito;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.beans.factory.annotation.Value;
78
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@@ -10,6 +11,7 @@
1011
import org.springframework.security.test.context.support.TestExecutionEvent;
1112
import org.springframework.security.test.context.support.WithUserDetails;
1213
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1315
import org.springframework.test.web.servlet.ResultActions;
1416
import org.springframework.transaction.annotation.Transactional;
1517
import org.springframework.transaction.support.TransactionTemplate;
@@ -25,6 +27,7 @@
2527
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
2628
import org.tuna.zoopzoop.backend.domain.space.space.repository.SpaceRepository;
2729
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
30+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
2831
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
2932

3033
import javax.crypto.Mac;
@@ -37,6 +40,7 @@
3740

3841
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
3942
import static org.awaitility.Awaitility.await;
43+
import static org.mockito.ArgumentMatchers.anyString;
4044
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4145
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
4246

@@ -56,6 +60,9 @@ class DashboardControllerTest extends ControllerTestSupport {
5660

5761
@Autowired private TransactionTemplate transactionTemplate;
5862

63+
@MockitoBean
64+
private LiveblocksClient liveblocksClient;
65+
5966
private Integer unauthorizedDashboardId;
6067
private Integer authorizedDashboardId;
6168

@@ -67,6 +74,9 @@ class DashboardControllerTest extends ControllerTestSupport {
6774

6875
@BeforeAll
6976
void setUp() {
77+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
78+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
79+
7080
// 1. 유저 생성
7181
memberService.createMember("tester1_forDashboardControllerTest", "url", "dc1111", Provider.KAKAO);
7282
memberService.createMember("tester2_forDashboardControllerTest", "url", "dc2222", Provider.KAKAO);

src/test/java/org/tuna/zoopzoop/backend/domain/dashboard/extraComponent/GraphUpdateConsumerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package org.tuna.zoopzoop.backend.domain.dashboard.extraComponent;
22

33
import org.junit.jupiter.api.*;
4+
import org.mockito.Mockito;
45
import org.springframework.amqp.rabbit.core.RabbitTemplate;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
79
import org.springframework.transaction.annotation.Transactional;
810
import org.springframework.transaction.support.TransactionTemplate;
911
import org.tuna.zoopzoop.backend.domain.dashboard.dto.GraphUpdateMessage;
@@ -12,13 +14,15 @@
1214
import org.tuna.zoopzoop.backend.domain.dashboard.entity.Node;
1315
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
1416
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
17+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
1518
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
1619

1720
import java.util.Map;
1821
import java.util.concurrent.TimeUnit;
1922

2023
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
2124
import static org.awaitility.Awaitility.await;
25+
import static org.mockito.ArgumentMatchers.anyString;
2226

2327
@SpringBootTest
2428
@Transactional
@@ -28,13 +32,19 @@ class GraphUpdateConsumerTest extends ControllerTestSupport {
2832
@Autowired private TransactionTemplate transactionTemplate;
2933
@Autowired private SpaceService spaceService;
3034

35+
@MockitoBean
36+
private LiveblocksClient liveblocksClient;
37+
3138
// 테스트에 사용할 dashboardId (실제 DB에 존재하는 ID)
3239
private Integer existingDashboardId;
3340

3441
private String existingSpaceName = "TestSpace1_forGraphUpdateConsumerTest";
3542

3643
@BeforeAll
3744
void setUp(){
45+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
46+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
47+
3848
spaceService.createSpace(existingSpaceName, "thumb1");
3949
}
4050

src/test/java/org/tuna/zoopzoop/backend/domain/space/archive/controller/SpaceArchiveFolderControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.junit.jupiter.api.*;
5+
import org.mockito.Mockito;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
78
import org.springframework.boot.test.context.SpringBootTest;
89
import org.springframework.http.MediaType;
910
import org.springframework.security.test.context.support.WithUserDetails;
1011
import org.springframework.test.context.ActiveProfiles;
12+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1113
import org.springframework.test.web.servlet.MockMvc;
1214
import org.springframework.transaction.annotation.Transactional;
1315
import org.tuna.zoopzoop.backend.domain.archive.folder.dto.reqBodyForCreateFolder;
@@ -25,13 +27,15 @@
2527
import org.tuna.zoopzoop.backend.domain.space.membership.service.MembershipService;
2628
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
2729
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
30+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
2831
import org.tuna.zoopzoop.backend.global.jpa.entity.BaseEntity;
2932

3033
import java.time.LocalDate;
3134
import java.util.List;
3235

3336
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
3437
import static org.hamcrest.Matchers.nullValue;
38+
import static org.mockito.ArgumentMatchers.anyString;
3539
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
3640
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3741
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -55,6 +59,9 @@ class SpaceArchiveFolderControllerTest {
5559
@Autowired private FolderRepository folderRepository;
5660
@Autowired private DataSourceRepository dataSourceRepository;
5761

62+
@MockitoBean
63+
private LiveblocksClient liveblocksClient;
64+
5865
private static final String OWNER_PK = "sp1111";
5966
private static final String READER_PK = "sp2222";
6067

@@ -67,6 +74,9 @@ class SpaceArchiveFolderControllerTest {
6774

6875
@BeforeAll
6976
void setUp() {
77+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
78+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
79+
7080
// 사용자 생성
7181
try { memberService.createMember("spaceOwner", "http://img/owner.png", OWNER_PK, Provider.KAKAO); } catch (Exception ignored) {}
7282
try { memberService.createMember("spaceReader", "http://img/reader.png", READER_PK, Provider.KAKAO); } catch (Exception ignored) {}

src/test/java/org/tuna/zoopzoop/backend/domain/space/membership/controller/ApiV1InviteControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import org.junit.jupiter.api.DisplayName;
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.TestInstance;
7+
import org.mockito.Mockito;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.security.test.context.support.TestExecutionEvent;
1112
import org.springframework.security.test.context.support.WithUserDetails;
1213
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1315
import org.springframework.test.web.servlet.ResultActions;
1416
import org.springframework.transaction.annotation.Transactional;
1517
import org.tuna.zoopzoop.backend.domain.member.enums.Provider;
@@ -18,8 +20,10 @@
1820
import org.tuna.zoopzoop.backend.domain.space.membership.service.MembershipService;
1921
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
2022
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
23+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
2124
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
2225

26+
import static org.mockito.ArgumentMatchers.anyString;
2327
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2428
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2529

@@ -36,8 +40,14 @@ class ApiV1InviteControllerTest extends ControllerTestSupport {
3640
@Autowired
3741
private MembershipService membershipService;
3842

43+
@MockitoBean
44+
private LiveblocksClient liveblocksClient;
45+
3946
@BeforeAll
4047
void setUp() {
48+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
49+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
50+
4151
setUpMember();
4252
setUpSpace();
4353
setUpMembership();

src/test/java/org/tuna/zoopzoop/backend/domain/space/membership/controller/ApiV1MembershipControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
import org.junit.jupiter.api.DisplayName;
55
import org.junit.jupiter.api.Test;
66
import org.junit.jupiter.api.TestInstance;
7+
import org.mockito.Mockito;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.security.test.context.support.TestExecutionEvent;
1112
import org.springframework.security.test.context.support.WithUserDetails;
1213
import org.springframework.test.context.ActiveProfiles;
14+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1315
import org.springframework.test.web.servlet.ResultActions;
1416
import org.springframework.transaction.annotation.Transactional;
1517
import org.tuna.zoopzoop.backend.domain.member.enums.Provider;
1618
import org.tuna.zoopzoop.backend.domain.member.service.MemberService;
1719
import org.tuna.zoopzoop.backend.domain.space.membership.enums.Authority;
1820
import org.tuna.zoopzoop.backend.domain.space.membership.service.MembershipService;
1921
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
22+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
2023
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
2124

25+
import static org.mockito.ArgumentMatchers.anyString;
2226
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2327
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2428

@@ -35,8 +39,14 @@ class ApiV1MembershipControllerTest extends ControllerTestSupport {
3539
@Autowired
3640
private MembershipService membershipService;
3741

42+
@MockitoBean
43+
private LiveblocksClient liveblocksClient;
44+
3845
@BeforeAll
3946
void setUp() {
47+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
48+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
49+
4050
setUpMember();
4151
setUpSpace();
4252
setUpMembership();

src/test/java/org/tuna/zoopzoop/backend/domain/space/membership/service/MembershipServiceTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import jakarta.persistence.NoResultException;
44
import org.junit.jupiter.api.*;
5+
import org.mockito.Mockito;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.context.SpringBootTest;
78
import org.springframework.dao.DataIntegrityViolationException;
89
import org.springframework.test.context.ActiveProfiles;
10+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
911
import org.springframework.transaction.annotation.Transactional;
1012
import org.tuna.zoopzoop.backend.domain.member.entity.Member;
1113
import org.tuna.zoopzoop.backend.domain.member.enums.Provider;
@@ -15,12 +17,14 @@
1517
import org.tuna.zoopzoop.backend.domain.space.membership.enums.Authority;
1618
import org.tuna.zoopzoop.backend.domain.space.membership.repository.MembershipRepository;
1719
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
20+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
1821

1922
import java.nio.file.AccessDeniedException;
2023
import java.util.List;
2124

2225
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2326
import static org.junit.jupiter.api.Assertions.*;
27+
import static org.mockito.ArgumentMatchers.anyString;
2428

2529
@ActiveProfiles("test")
2630
@SpringBootTest
@@ -38,8 +42,14 @@ class MembershipServiceTest {
3842
@Autowired
3943
private MemberRepository memberRepository;
4044

45+
@MockitoBean
46+
private LiveblocksClient liveblocksClient;
47+
4148
@BeforeAll
4249
void setUp() {
50+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
51+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
52+
4353
membershipRepository.deleteAll();
4454
setUpMember();
4555
setUpSpace();

src/test/java/org/tuna/zoopzoop/backend/domain/space/space/controller/ApiV1SpaceControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.tuna.zoopzoop.backend.domain.space.space.controller;
22

33
import org.junit.jupiter.api.*;
4+
import org.mockito.Mockito;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
67
import org.springframework.boot.test.context.SpringBootTest;
78
import org.springframework.security.test.context.support.TestExecutionEvent;
89
import org.springframework.security.test.context.support.WithUserDetails;
910
import org.springframework.test.context.ActiveProfiles;
11+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1012
import org.springframework.test.web.servlet.ResultActions;
1113
import org.springframework.transaction.annotation.Transactional;
1214
import org.tuna.zoopzoop.backend.domain.member.enums.Provider;
@@ -15,10 +17,12 @@
1517
import org.tuna.zoopzoop.backend.domain.space.membership.service.MembershipService;
1618
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
1719
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
20+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
1821
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
1922

2023
import static org.hamcrest.Matchers.nullValue;
2124
import static org.hamcrest.Matchers.startsWith;
25+
import static org.mockito.ArgumentMatchers.anyString;
2226
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
2327
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2428
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@@ -36,8 +40,14 @@ class ApiV1SpaceControllerTest extends ControllerTestSupport {
3640
@Autowired
3741
private MembershipService membershipService;
3842

43+
@MockitoBean
44+
private LiveblocksClient liveblocksClient;
45+
3946
@BeforeAll
4047
void setUp() {
48+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
49+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
50+
4151
setUpMember();
4252
setUpSpace();
4353
setUpMembership();

src/test/java/org/tuna/zoopzoop/backend/domain/space/space/service/SpaceServiceTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import org.junit.jupiter.api.BeforeEach;
66
import org.junit.jupiter.api.DisplayName;
77
import org.junit.jupiter.api.Test;
8+
import org.mockito.Mockito;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.test.context.SpringBootTest;
1011
import org.springframework.test.context.ActiveProfiles;
12+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
1113
import org.springframework.transaction.annotation.Transactional;
1214
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
1315
import org.tuna.zoopzoop.backend.domain.space.space.exception.DuplicateSpaceNameException;
16+
import org.tuna.zoopzoop.backend.global.clients.liveblocks.LiveblocksClient;
1417

1518
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
19+
import static org.mockito.ArgumentMatchers.anyString;
1620

1721
@ActiveProfiles("test")
1822
@SpringBootTest
@@ -21,8 +25,12 @@ class SpaceServiceTest {
2125
@Autowired
2226
private SpaceService spaceService;
2327

28+
@MockitoBean
29+
private LiveblocksClient liveblocksClient;
30+
2431
@BeforeEach
2532
void setUp() {
33+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
2634
spaceService.createSpace("기존 스페이스 1_forSpaceServiceTest");
2735
spaceService.createSpace("기존 스페이스 2_forSpaceServiceTest");
2836
}
@@ -34,6 +42,9 @@ void createSpace_Success() {
3442
// Given
3543
String spaceName = "테스트 스페이스";
3644

45+
// liveblocksClient.createRoom() 메서드가 호출될 때 실제로 아무 작업도 하지 않도록 설정
46+
Mockito.doNothing().when(liveblocksClient).createRoom(anyString());
47+
3748
// When
3849
var createdSpace = spaceService.createSpace(spaceName);
3950

@@ -74,6 +85,9 @@ void deleteSpace_Success() {
7485
Integer spaceId = space.getId();
7586
String spaceName = space.getName();
7687

88+
// Mockito 설정: deleteRoom 메소드가 어떤 문자열로 호출되더라도 아무런 동작을 하지 않도록 설정
89+
Mockito.doNothing().when(liveblocksClient).deleteRoom(anyString());
90+
7791
// When
7892
String deletedSpaceName = spaceService.deleteSpace(spaceId);
7993

0 commit comments

Comments
 (0)