Skip to content

Commit b0c8baa

Browse files
author
EpicFn
committed
fix : 테스트 코드 수정 중
1 parent 4550585 commit b0c8baa

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010
import org.springframework.http.MediaType;
1111
import org.springframework.security.test.context.support.TestExecutionEvent;
1212
import org.springframework.security.test.context.support.WithUserDetails;
13+
import org.springframework.test.annotation.DirtiesContext;
1314
import org.springframework.test.context.ActiveProfiles;
14-
import org.springframework.test.web.servlet.MockMvc;
15+
import org.springframework.test.context.jdbc.Sql;
1516
import org.springframework.test.web.servlet.ResultActions;
1617
import org.springframework.transaction.annotation.Transactional;
1718
import org.springframework.transaction.support.TransactionTemplate;
1819
import org.tuna.zoopzoop.backend.domain.dashboard.entity.Graph;
19-
import org.tuna.zoopzoop.backend.domain.dashboard.repository.DashboardRepository;
20-
import org.tuna.zoopzoop.backend.domain.dashboard.repository.GraphRepository;
2120
import org.tuna.zoopzoop.backend.domain.member.enums.Provider;
21+
import org.tuna.zoopzoop.backend.domain.member.repository.MemberRepository;
2222
import org.tuna.zoopzoop.backend.domain.member.service.MemberService;
2323
import org.tuna.zoopzoop.backend.domain.space.membership.enums.Authority;
24+
import org.tuna.zoopzoop.backend.domain.space.membership.repository.MembershipRepository;
2425
import org.tuna.zoopzoop.backend.domain.space.membership.service.MembershipService;
2526
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
27+
import org.tuna.zoopzoop.backend.domain.space.space.repository.SpaceRepository;
2628
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
2729
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
2830

@@ -35,11 +37,8 @@
3537

3638
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
3739
import static org.awaitility.Awaitility.await;
38-
import static org.hamcrest.Matchers.hasSize;
39-
import static org.junit.jupiter.api.Assertions.assertEquals;
4040
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
4141
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
42-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
4342

4443
@SpringBootTest
4544
@AutoConfigureMockMvc
@@ -48,23 +47,21 @@
4847
@Transactional
4948
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
5049
class DashboardControllerTest extends ControllerTestSupport {
51-
@Autowired
52-
private SpaceService spaceService;
50+
@Autowired private SpaceService spaceService;
51+
@Autowired private MemberService memberService;
52+
@Autowired private MembershipService membershipService;
5353

54-
@Autowired
55-
private MemberService memberService;
54+
@Autowired SpaceRepository spaceRepository;
55+
@Autowired MemberRepository memberRepository;
56+
@Autowired MembershipRepository membershipRepository;
5657

57-
@Autowired
58-
private MembershipService membershipService;
58+
@Autowired private TransactionTemplate transactionTemplate;
5959

60-
@Autowired
61-
private DashboardRepository dashboardRepository;
62-
63-
@Autowired
64-
private TransactionTemplate transactionTemplate;
65-
66-
private Integer authorizedDashboardId;
6760
private Integer unauthorizedDashboardId;
61+
private Integer authorizedDashboardId;
62+
63+
private String authorizedSpaceName = "TestSpace1_forDashboardControllerTest";
64+
private String unauthorizedSpaceName = "TestSpace2_forDashboardControllerTest";
6865

6966
@Value("${liveblocks.secret-key}")
7067
private String testSecretKey;
@@ -76,8 +73,8 @@ void setUp() {
7673
memberService.createMember("tester2_forDashboardControllerTest", "url", "dc2222", Provider.KAKAO);
7774

7875
// 2. 스페이스 생성 (생성과 동시에 대시보드도 생성됨)
79-
Space space1 = spaceService.createSpace("TestSpace1_forDashboardControllerTest", "thumb1");
80-
Space space2 = spaceService.createSpace("TestSpace2_forDashboardControllerTest", "thumb2");
76+
Space space1 = spaceService.createSpace(authorizedSpaceName, "thumb1");
77+
Space space2 = spaceService.createSpace(unauthorizedSpaceName, "thumb2");
8178

8279
// 테스트에서 사용할 대시보드 ID 저장
8380
this.authorizedDashboardId = space1.getDashboard().getId();
@@ -98,6 +95,14 @@ void setUp() {
9895
);
9996
}
10097

98+
@AfterAll
99+
void tearDown() {
100+
// 멤버십, 스페이스, 멤버 모두 삭제
101+
membershipRepository.deleteAll();
102+
spaceRepository.deleteAll();
103+
memberRepository.deleteAll();
104+
}
105+
101106
// ============================= GET GRAPH ============================= //
102107

103108
@Test
@@ -180,7 +185,8 @@ void updateGraph_Success() throws Exception {
180185
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
181186
// [수정] transactionTemplate을 사용하여 트랜잭션 내에서 검증 로직을 실행
182187
transactionTemplate.execute(status -> {
183-
Graph updatedGraph = dashboardRepository.findById(authorizedDashboardId).get().getGraph();
188+
Space space = spaceService.findByName(authorizedSpaceName);
189+
Graph updatedGraph = space.getDashboard().getGraph();
184190
assertThat(updatedGraph.getNodes()).hasSize(2);
185191
assertThat(updatedGraph.getEdges()).hasSize(1);
186192
assertThat(updatedGraph.getNodes().get(0).getData().get("title")).isEqualTo("노드1");

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

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

3-
import org.junit.jupiter.api.BeforeEach;
4-
import org.junit.jupiter.api.DisplayName;
5-
import org.junit.jupiter.api.Test;
3+
import org.assertj.core.api.AbstractStringAssert;
4+
import org.junit.jupiter.api.*;
65
import org.springframework.amqp.rabbit.core.RabbitTemplate;
76
import org.springframework.beans.factory.annotation.Autowired;
87
import org.springframework.boot.test.context.SpringBootTest;
9-
import org.springframework.test.web.servlet.ResultActions;
8+
import org.springframework.test.annotation.DirtiesContext;
9+
import org.springframework.test.context.jdbc.Sql;
1010
import org.springframework.transaction.annotation.Transactional;
11+
import org.springframework.transaction.support.TransactionTemplate;
1112
import org.tuna.zoopzoop.backend.domain.dashboard.dto.GraphUpdateMessage;
1213
import org.tuna.zoopzoop.backend.domain.dashboard.entity.Graph;
13-
import org.tuna.zoopzoop.backend.domain.dashboard.repository.DashboardRepository;
1414
import org.tuna.zoopzoop.backend.domain.space.space.entity.Space;
1515
import org.tuna.zoopzoop.backend.domain.space.space.service.SpaceService;
1616
import org.tuna.zoopzoop.backend.testSupport.ControllerTestSupport;
@@ -22,30 +22,29 @@
2222

2323
@SpringBootTest
2424
@Transactional
25+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
2526
class GraphUpdateConsumerTest extends ControllerTestSupport {
26-
@Autowired
27-
private RabbitTemplate rabbitTemplate;
28-
29-
@Autowired
30-
private DashboardRepository dashboardRepository;
31-
32-
@Autowired
33-
SpaceService spaceService;
27+
@Autowired private RabbitTemplate rabbitTemplate;
28+
@Autowired private TransactionTemplate transactionTemplate;
29+
@Autowired SpaceService spaceService;
3430

3531
// 테스트에 사용할 dashboardId (실제 DB에 존재하는 ID)
3632
private Integer existingDashboardId;
3733

38-
@BeforeEach
34+
private String existingSpaceName = "TestSpace1_forGraphUpdateConsumerTest";
35+
36+
@BeforeAll
3937
void setUp(){
40-
spaceService.createSpace("TestSpace1_forGraphUpdateConsumerTest", "thumb1");
38+
spaceService.createSpace(existingSpaceName, "thumb1");
4139
}
4240

41+
4342
@Test
4443
@DisplayName("큐에 업데이트 메시지가 들어오면 컨슈머가 DB를 성공적으로 업데이트한다")
4544
void handleGraphUpdate_Success() throws Exception {
4645
// Given
4746
// ControllerTestSupport의 setUp에서 생성된 대시보드 ID를 가져옵니다.
48-
existingDashboardId = spaceService.findByName("TestSpace1_forGraphUpdateConsumerTest").getDashboard().getId();
47+
existingDashboardId = spaceService.findByName(existingSpaceName).getDashboard().getId();
4948

5049
String requestBody = createReactFlowJsonBody(); // 테스트용 JSON 데이터
5150
GraphUpdateMessage message = new GraphUpdateMessage(existingDashboardId, requestBody);
@@ -55,9 +54,14 @@ void handleGraphUpdate_Success() throws Exception {
5554

5655
// Then: 컨슈머가 메시지를 처리하여 DB가 변경될 때까지 최대 5초간 기다립니다.
5756
await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
58-
Graph updatedGraph = dashboardRepository.findById(existingDashboardId).get().getGraph();
59-
assertThat(updatedGraph.getNodes()).hasSize(2);
60-
assertThat(updatedGraph.getNodes().get(0).getData().get("title")).isEqualTo("노드1");
57+
transactionTemplate.execute(status -> {
58+
Space space = spaceService.findByName(existingSpaceName);
59+
Graph updatedGraph = space.getDashboard().getGraph();
60+
assertThat(updatedGraph.getNodes()).hasSize(2);
61+
assertThat(updatedGraph.getEdges()).hasSize(1);
62+
assertThat(updatedGraph.getNodes().get(0).getData().get("title")).isEqualTo("노드1");
63+
return null;
64+
});
6165
});
6266
}
6367

0 commit comments

Comments
 (0)