Skip to content

Commit 435de08

Browse files
committed
test: 실습 1부 내용 반영
1 parent eb2ed45 commit 435de08

22 files changed

+694
-19
lines changed

src/main/java/com/example/demo/controller/PostController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public class PostController {
2727
public ResponseEntity<PostResponse> getPostById(@PathVariable long id) {
2828
return ResponseEntity
2929
.ok()
30-
.body(toResponse(postService.getPostById(id)));
30+
.body(toResponse(postService.getById(id)));
3131
}
3232

3333
@PutMapping("/{id}")
3434
public ResponseEntity<PostResponse> updatePost(@PathVariable long id, @RequestBody PostUpdateDto postUpdateDto) {
3535
return ResponseEntity
3636
.ok()
37-
.body(toResponse(postService.updatePost(id, postUpdateDto)));
37+
.body(toResponse(postService.update(id, postUpdateDto)));
3838
}
3939

4040
public PostResponse toResponse(PostEntity postEntity) {

src/main/java/com/example/demo/controller/PostCreateController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public class PostCreateController {
2525
public ResponseEntity<PostResponse> createPost(@RequestBody PostCreateDto postCreateDto) {
2626
return ResponseEntity
2727
.status(HttpStatus.CREATED)
28-
.body(postController.toResponse(postService.createPost(postCreateDto)));
28+
.body(postController.toResponse(postService.create(postCreateDto)));
2929
}
3030
}

src/main/java/com/example/demo/controller/UserController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class UserController {
3535
public ResponseEntity<UserResponse> getUserById(@PathVariable long id) {
3636
return ResponseEntity
3737
.ok()
38-
.body(toResponse(userService.getByIdOrElseThrow(id)));
38+
.body(toResponse(userService.getById(id)));
3939
}
4040

4141
@GetMapping("/{id}/verify")
@@ -68,7 +68,7 @@ public ResponseEntity<MyProfileResponse> updateMyInfo(
6868
@RequestBody UserUpdateDto userUpdateDto
6969
) {
7070
UserEntity userEntity = userService.getByEmail(email);
71-
userEntity = userService.updateUser(userEntity.getId(), userUpdateDto);
71+
userEntity = userService.update(userEntity.getId(), userUpdateDto);
7272
return ResponseEntity
7373
.ok()
7474
.body(toMyProfileResponse(userEntity));

src/main/java/com/example/demo/controller/UserCreateController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class UserCreateController {
2424

2525
@PostMapping
2626
public ResponseEntity<UserResponse> createUser(@RequestBody UserCreateDto userCreateDto) {
27-
UserEntity userEntity = userService.createUser(userCreateDto);
27+
UserEntity userEntity = userService.create(userCreateDto);
2828
return ResponseEntity
2929
.status(HttpStatus.CREATED)
3030
.body(userController.toResponse(userEntity));

src/main/java/com/example/demo/service/PostService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ public class PostService {
1717
private final PostRepository postRepository;
1818
private final UserService userService;
1919

20-
public PostEntity getPostById(long id) {
20+
public PostEntity getById(long id) {
2121
return postRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Posts", id));
2222
}
2323

24-
public PostEntity createPost(PostCreateDto postCreateDto) {
25-
UserEntity userEntity = userService.getByIdOrElseThrow(postCreateDto.getWriterId());
24+
public PostEntity create(PostCreateDto postCreateDto) {
25+
UserEntity userEntity = userService.getById(postCreateDto.getWriterId());
2626
PostEntity postEntity = new PostEntity();
2727
postEntity.setWriter(userEntity);
2828
postEntity.setContent(postCreateDto.getContent());
2929
postEntity.setCreatedAt(Clock.systemUTC().millis());
3030
return postRepository.save(postEntity);
3131
}
3232

33-
public PostEntity updatePost(long id, PostUpdateDto postUpdateDto) {
34-
PostEntity postEntity = getPostById(id);
33+
public PostEntity update(long id, PostUpdateDto postUpdateDto) {
34+
PostEntity postEntity = getById(id);
3535
postEntity.setContent(postUpdateDto.getContent());
3636
postEntity.setModifiedAt(Clock.systemUTC().millis());
3737
return postRepository.save(postEntity);

src/main/java/com/example/demo/service/UserService.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,18 @@ public class UserService {
2323
private final UserRepository userRepository;
2424
private final JavaMailSender mailSender;
2525

26-
public Optional<UserEntity> getById(long id) {
27-
return userRepository.findByIdAndStatus(id, UserStatus.ACTIVE);
28-
}
29-
3026
public UserEntity getByEmail(String email) {
3127
return userRepository.findByEmailAndStatus(email, UserStatus.ACTIVE)
3228
.orElseThrow(() -> new ResourceNotFoundException("Users", email));
3329
}
3430

35-
public UserEntity getByIdOrElseThrow(long id) {
31+
public UserEntity getById(long id) {
3632
return userRepository.findByIdAndStatus(id, UserStatus.ACTIVE)
3733
.orElseThrow(() -> new ResourceNotFoundException("Users", id));
3834
}
3935

4036
@Transactional
41-
public UserEntity createUser(UserCreateDto userCreateDto) {
37+
public UserEntity create(UserCreateDto userCreateDto) {
4238
UserEntity userEntity = new UserEntity();
4339
userEntity.setEmail(userCreateDto.getEmail());
4440
userEntity.setNickname(userCreateDto.getNickname());
@@ -52,8 +48,8 @@ public UserEntity createUser(UserCreateDto userCreateDto) {
5248
}
5349

5450
@Transactional
55-
public UserEntity updateUser(long id, UserUpdateDto userUpdateDto) {
56-
UserEntity userEntity = getByIdOrElseThrow(id);
51+
public UserEntity update(long id, UserUpdateDto userUpdateDto) {
52+
UserEntity userEntity = getById(id);
5753
userEntity.setNickname(userUpdateDto.getNickname());
5854
userEntity.setAddress(userUpdateDto.getAddress());
5955
userEntity = userRepository.save(userEntity);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.example.demo.controller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
5+
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
9+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.web.servlet.MockMvc;
12+
13+
@SpringBootTest
14+
@AutoConfigureMockMvc
15+
@AutoConfigureTestDatabase
16+
public class HealthCheckTest {
17+
18+
@Autowired
19+
private MockMvc mockMvc;
20+
21+
@Test
22+
void 헬스_체크_응답이_200으로_내려온다() throws Exception {
23+
// given
24+
// when
25+
// then
26+
mockMvc.perform(get("/health_check.html"))
27+
.andExpect(status().isOk());
28+
}
29+
30+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.example.demo.controller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
4+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
6+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
7+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
8+
9+
import com.example.demo.model.dto.PostUpdateDto;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
14+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
15+
import org.springframework.boot.test.context.SpringBootTest;
16+
import org.springframework.http.MediaType;
17+
import org.springframework.test.context.jdbc.Sql;
18+
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
19+
import org.springframework.test.context.jdbc.SqlGroup;
20+
import org.springframework.test.web.servlet.MockMvc;
21+
22+
@SpringBootTest
23+
@AutoConfigureMockMvc
24+
@AutoConfigureTestDatabase
25+
@SqlGroup({
26+
@Sql(value = "/sql/post-controller-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
27+
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
28+
})
29+
public class PostControllerTest {
30+
31+
@Autowired
32+
private MockMvc mockMvc;
33+
private final ObjectMapper objectMapper = new ObjectMapper();
34+
35+
@Test
36+
void 사용자는_게시물을_단건_조회_할_수_있다() throws Exception {
37+
// given
38+
// when
39+
// then
40+
mockMvc.perform(get("/api/posts/1"))
41+
.andExpect(status().isOk())
42+
.andExpect(jsonPath("$.id").isNumber())
43+
.andExpect(jsonPath("$.content").value("helloworld"))
44+
.andExpect(jsonPath("$.writer.id").isNumber())
45+
.andExpect(jsonPath("$.writer.email").value("[email protected]"))
46+
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
47+
}
48+
49+
@Test
50+
void 사용자가_존재하지_않는_게시물을_조회할_경우_에러가_난다() throws Exception {
51+
// given
52+
// when
53+
// then
54+
mockMvc.perform(get("/api/posts/123456789"))
55+
.andExpect(status().isNotFound())
56+
.andExpect(content().string("Posts에서 ID 123456789를 찾을 수 없습니다."));
57+
}
58+
59+
@Test
60+
void 사용자는_게시물을_수정할_수_있다() throws Exception {
61+
// given
62+
PostUpdateDto postUpdateDto = PostUpdateDto.builder()
63+
.content("foobar")
64+
.build();
65+
66+
// when
67+
// then
68+
mockMvc.perform(
69+
put("/api/posts/1")
70+
.contentType(MediaType.APPLICATION_JSON)
71+
.content(objectMapper.writeValueAsString(postUpdateDto)))
72+
.andExpect(status().isOk())
73+
.andExpect(jsonPath("$.id").isNumber())
74+
.andExpect(jsonPath("$.content").value("foobar"))
75+
.andExpect(jsonPath("$.writer.id").isNumber())
76+
.andExpect(jsonPath("$.writer.email").value("[email protected]"))
77+
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
78+
}
79+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.demo.controller;
2+
3+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
5+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6+
7+
import com.example.demo.model.dto.PostCreateDto;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
12+
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.test.context.jdbc.Sql;
16+
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
17+
import org.springframework.test.context.jdbc.SqlGroup;
18+
import org.springframework.test.web.servlet.MockMvc;
19+
20+
@SpringBootTest
21+
@AutoConfigureMockMvc
22+
@AutoConfigureTestDatabase
23+
@SqlGroup({
24+
@Sql(value = "/sql/post-create-controller-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD),
25+
@Sql(value = "/sql/delete-all-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
26+
})
27+
public class PostCreateControllerTest {
28+
29+
@Autowired
30+
private MockMvc mockMvc;
31+
private final ObjectMapper objectMapper = new ObjectMapper();
32+
33+
@Test
34+
void 사용자는_게시물을_작성할_수_있다() throws Exception {
35+
// given
36+
PostCreateDto postCreateDto = PostCreateDto.builder()
37+
.writerId(1)
38+
.content("helloworld")
39+
.build();
40+
41+
// when
42+
// then
43+
mockMvc.perform(
44+
post("/api/posts")
45+
.contentType(MediaType.APPLICATION_JSON)
46+
.content(objectMapper.writeValueAsString(postCreateDto)))
47+
.andExpect(status().isCreated())
48+
.andExpect(jsonPath("$.id").isNumber())
49+
.andExpect(jsonPath("$.content").value("helloworld"))
50+
.andExpect(jsonPath("$.writer.id").isNumber())
51+
.andExpect(jsonPath("$.writer.email").value("[email protected]"))
52+
.andExpect(jsonPath("$.writer.nickname").value("kok202"));
53+
}
54+
}

0 commit comments

Comments
 (0)