Skip to content

Commit 517ee44

Browse files
committed
test: 서비스, 레포지토리, 컨트롤러 및 통합 테스트 추가
1 parent a37d849 commit 517ee44

19 files changed

+2068
-116
lines changed

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ dependencies {
6262
// Spring Boot Test 의존성
6363
testImplementation 'org.springframework.boot:spring-boot-starter-test'
6464

65+
// Mockito 의존성
66+
implementation "org.mockito:mockito-core:4.6.1"
67+
6568
// JUnit 플랫폼 런처 (JUnit 테스트 실행)
6669
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
6770

error-log/stub 인수 불일치 문제.md

Whitespace-only changes.

src/main/java/com/sprint/mission/discodeit/DiscodeitApplication.java

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
66

77
@SpringBootApplication
8-
@EnableJpaAuditing
98
public class DiscodeitApplication {
109

1110
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sprint.mission.discodeit.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
5+
6+
@Configuration
7+
@EnableJpaAuditing // @WebMvcTest 와의 충돌을 고려해 분리
8+
public class JpaConfig {
9+
10+
}

src/main/java/com/sprint/mission/discodeit/controller/UserController.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public ResponseEntity<UserDto> createUser(
3636
/* 유저 생성 요청(Request) */
3737
log.info("유저 생성 요청(Request): username={}, hasProfileImage={}",
3838
userCreateRequest.username(),
39-
!file.isEmpty());
39+
file != null);
4040

4141
// 프로필 이미지 처리
4242
/* TODO(멘토님께) : 삼항 연산자를 쓰면 가독성이 떨어진다는 이야기를 들었는데,

src/main/java/com/sprint/mission/discodeit/service/basic/BasicChannelService.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public ChannelDto createPublicChannel(ChannelPublicRequest request) {
5353
.name(request.name())
5454
.description(request.description())
5555
.build();
56-
channelRepository.save(channel);
56+
channel = channelRepository.save(channel);
5757

5858
log.info("공개 채널 생성 성공: channelName={}, createdAt={}",
5959
channel.getName(),
@@ -68,7 +68,8 @@ public ChannelDto createPrivateChannel(ChannelPrivateRequest request) {
6868
Channel channel = Channel.builder()
6969
.type(ChannelType.PRIVATE)
7070
.build();
71-
channelRepository.save(channel);
71+
Channel savedChannel = channelRepository.save(channel);
72+
log.info("채널 저장 성공, ID: {}", savedChannel.getId());
7273

7374
request.participantIds().stream()
7475
.map(userId -> ReadStatus.builder()
@@ -77,22 +78,25 @@ public ChannelDto createPrivateChannel(ChannelPrivateRequest request) {
7778
log.error("비공개 채널 생성 단계에서 유저를 찾지 못함: userId={}", userId);
7879
return new UserNotFoundException(Map.of("UserId", userId));
7980
}))
80-
.channel(channelRepository.findById(channel.getId()).orElseThrow(
81+
.channel(channelRepository.findById(savedChannel.getId()).orElseThrow(
8182
() -> {
82-
log.error("비공개 채널을 찾지 못함: privateChannelId={}", channel.getId());
83-
return new ChannelNotFoundException(Map.of("channelId", channel.getId()));
83+
log.error("비공개 채널을 찾지 못함: privateChannelId={}", savedChannel.getId());
84+
return new ChannelNotFoundException(Map.of("channelId", savedChannel.getId()));
8485
}))
85-
.lastReadAt(channel.getCreatedAt())
86+
.lastReadAt(savedChannel.getCreatedAt())
8687
.build()
8788
)
8889
.forEach(readStatusRepository::save);
8990

9091
log.info("비공개 채널 생성 성공");
91-
return channelMapper.toDto(channel);
92+
return channelMapper.toDto(savedChannel);
9293
}
9394

9495
@Override
9596
public List<ChannelDto> findAllByUserId(UUID userId) {
97+
userRepository.findById(userId).orElseThrow(() -> {
98+
return new UserNotFoundException(Map.of("userId", userId));
99+
});
96100
List<Channel> channels = readStatusService.findAllReadStatusEntitiesByUserId(userId).stream()
97101
.map(ReadStatus::getChannel)
98102
.toList();
@@ -127,10 +131,14 @@ public ChannelDto updateChannel(UUID id, ChannelUpdateRequest request) {
127131
throw new ChannelModificationNotAllowedException(Map.of("privateChannelId", id));
128132
} // 전역 400
129133

130-
channel.updateName(request.newName());
131-
channel.updateDescription(request.newDescription());
132-
// 수정 시간 업데이트
133-
channel.refreshUpdateAt();
134+
if (request.newName() != null) {
135+
channel.updateName(request.newName());
136+
channel.refreshUpdateAt();
137+
}
138+
if (request.newDescription() != null) {
139+
channel.updateDescription(request.newDescription());
140+
channel.refreshUpdateAt();
141+
}
134142

135143
log.info("채널 수정 시도 성공: channelName={}, updatedAt={}",
136144
channel.getName(),

src/main/java/com/sprint/mission/discodeit/service/basic/BasicMessageService.java

+4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ public MessageDto createMessage(MessageCreateRequest messageCreateRequest,
101101
public PageResponse<MessageDto> findAllByChannelId(UUID channelId, Pageable pageable) {
102102
// 왜 이렇게 변환하는거지 (이해가 필요...)
103103

104+
channelRepository.findById(channelId).orElseThrow(() -> {
105+
return new ChannelNotFoundException(Map.of("channelId", channelId));
106+
});
107+
104108
// 페이징된 데이터 조회
105109
Page<Message> messagePage = messageRepository.findByChannelId(channelId, pageable);
106110

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
spring:
2+
datasource:
3+
url: jdbc:h2:mem:devDB;MODE=PostgreSQL # PostgreSQL 호환 모드 추가
4+
username: devUser
5+
password: devPass
6+
driver-class-name: org.h2.Driver # 명시적으로 드라이버 클래스 지정
7+
h2:
8+
console:
9+
enabled: true # H2 콘솔 활성화
10+
path: /h2-console
11+
sql:
12+
init:
13+
mode: always # Spring Boot DB 테이블 만들기
14+
jpa:
15+
hibernate:
16+
ddl-auto: none
17+
properties:
18+
hibernate:
19+
highlight_sql: true # 텍스트에 컬러 입히기
20+
format_sql: true # sql 포맷을 깔끔하게
21+
22+
logging:
23+
level:
24+
root: INFO
25+
org.hibernate.SQL: DEBUG # Hibernate SQL 쿼리 로그만 DEBUG 활성화, Logger 로 출력
26+
org.hibernate.orm.jdbc.bind: TRACE # 바인딩된 파라미터 표시 로그
27+
server:
28+
port: 8080

src/main/resources/application.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ spring:
33
name: discodeit
44

55
profiles:
6-
active: prod
6+
active: test
77

88
jpa:
99
show-sql: true
@@ -48,8 +48,8 @@ info:
4848
spring-boot-version: 3.4.0
4949

5050
datasource:
51-
url: jdbc:postgresql://localhost:5432/discodeit
52-
driver-class-name: org.postgresql.Driver
51+
url: jdbc:h2:mem:devDB;MODE=PostgreSQL
52+
driver-class-name: org.h2.Driver
5353

5454
jpa:
5555
ddl-auto: validate

0 commit comments

Comments
 (0)