Skip to content

Commit

Permalink
refactor: API 테스트 리팩터링
Browse files Browse the repository at this point in the history
  • Loading branch information
hong-sile committed Nov 21, 2023
1 parent 90b5138 commit e288371
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.emmsale.activity.application.dto.ActivityResponse;
import com.emmsale.member.api.MemberApi;
import com.emmsale.member.application.dto.DescriptionRequest;
import com.emmsale.member.application.dto.MemberActivityAddRequest;
Expand Down Expand Up @@ -95,10 +96,10 @@ void addActivity() throws Exception {
final List<Long> activityIds = List.of(4L, 5L, 6L);
final MemberActivityAddRequest request = new MemberActivityAddRequest(activityIds);

final List<MemberActivityResponse> memberActivityResponses = createMemberActivityResponses();
final List<ActivityResponse> activityResponses = createActivityResponses();

when(memberActivityCommandService.addActivity(any(), any()))
.thenReturn(memberActivityResponses);
.thenReturn(activityResponses);

//when & then
mockMvc.perform(post("/members/activities")
Expand All @@ -111,14 +112,14 @@ void addActivity() throws Exception {
MEMBER_ACTIVITY_RESPONSE_FIELDS));
}

private List<MemberActivityResponse> createMemberActivityResponses() {
private List<ActivityResponse> createActivityResponses() {
return List.of(
new MemberActivityResponse(1L, "YAPP", "동아리"),
new MemberActivityResponse(2L, "DND", "동아리"),
new MemberActivityResponse(3L, "nexters", "동아리"),
new MemberActivityResponse(4L, "인프콘", "컨퍼런스"),
new MemberActivityResponse(5L, "우아한테크코스", "교육"),
new MemberActivityResponse(6L, "Backend", "직무")
new ActivityResponse(1L, "YAPP", "동아리"),
new ActivityResponse(2L, "DND", "동아리"),
new ActivityResponse(3L, "nexters", "동아리"),
new ActivityResponse(4L, "인프콘", "컨퍼런스"),
new ActivityResponse(5L, "우아한테크코스", "교육"),
new ActivityResponse(6L, "Backend", "직무")
);
}

Expand All @@ -128,12 +129,12 @@ void test_deleteActivity() throws Exception {
//given
final String activityIds = "1,2";

final List<MemberActivityResponse> memberActivityResponses = List.of(
new MemberActivityResponse(3L, "nexters", "동아리")
final List<ActivityResponse> activityResponses = List.of(
new ActivityResponse(3L, "nexters", "동아리")
);

when(memberActivityCommandService.deleteActivity(any(), any()))
.thenReturn(memberActivityResponses);
.thenReturn(activityResponses);

//when & then
mockMvc.perform(
Expand All @@ -149,11 +150,10 @@ void test_deleteActivity() throws Exception {
@DisplayName("내 활동들을 조회할 수 있다.")
void test_findActivity() throws Exception {
//given
final List<MemberActivityResponse> memberActivityResponse = createMemberActivityResponses();
final List<ActivityResponse> memberActivityResponse = createActivityResponses();

//when
when(memberActivityQueryService.findActivities(any()))
.thenReturn(memberActivityResponse);
when(memberActivityQueryService.findActivities(any())).thenReturn(memberActivityResponse);

//then
mockMvc.perform(get("/members/1/activities")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.emmsale.activity.application.dto;

import com.emmsale.activity.domain.Activity;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@RequiredArgsConstructor
@ToString
@Getter
public class ActivityResponse {

private final Long id;
Expand All @@ -12,21 +16,9 @@ public class ActivityResponse {

public static ActivityResponse from(final Activity activity) {
return new ActivityResponse(
activity.getId(),
activity.getActivityType().getValue(),
activity.getName()
activity.getId(),
activity.getActivityType().getValue(),
activity.getName()
);
}

public Long getId() {
return id;
}

public String getActivityType() {
return activityType;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emmsale.member.api;

import com.emmsale.activity.application.dto.ActivityResponse;
import com.emmsale.member.application.MemberActivityCommandService;
import com.emmsale.member.application.MemberActivityQueryService;
import com.emmsale.member.application.MemberCommandService;
Expand Down Expand Up @@ -46,7 +47,7 @@ public ResponseEntity<Void> register(
}

@PostMapping("/members/activities")
public ResponseEntity<List<MemberActivityResponse>> addActivity(
public ResponseEntity<List<ActivityResponse>> addActivity(
final Member member,
@RequestBody final MemberActivityAddRequest memberActivityAddRequest
) {
Expand All @@ -55,14 +56,14 @@ public ResponseEntity<List<MemberActivityResponse>> addActivity(
}

@DeleteMapping("/members/activities")
public ResponseEntity<List<MemberActivityResponse>> deleteActivity(final Member member,
public ResponseEntity<List<ActivityResponse>> deleteActivity(final Member member,
@RequestParam("activity-ids") final List<Long> deleteActivityIds) {
return ResponseEntity.ok(
memberActivityCommandService.deleteActivity(member, deleteActivityIds));
}

@GetMapping("/members/{member-id}/activities")
public ResponseEntity<List<MemberActivityResponse>> findActivity(
public ResponseEntity<List<ActivityResponse>> findActivity(
@PathVariable("member-id") final Long memberId) {
return ResponseEntity.ok(memberActivityQueryService.findActivities(memberId));
}
Expand Down

0 comments on commit e288371

Please sign in to comment.