Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: 테스트 코드 최적화 및 패키지 분리를 한다 #437

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom;


import com.digginroom.digginroom.util.DatabaseCleanerExtension;
Expand All @@ -20,7 +20,7 @@ public abstract class ControllerTest {
private int port;

@BeforeEach
void setUp() {
void setPortUp() {
RestAssured.port = port;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@SpringBootTest
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class DigginRoomApplicationTests {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package com.digginroom.digginroom.controller;

import static com.digginroom.digginroom.TestFixture.COMMENT_UPDATE_REQUEST;
import static com.digginroom.digginroom.TestFixture.MEMBER2_LOGIN_REQUEST;
import static com.digginroom.digginroom.TestFixture.MEMBER_LOGIN_REQUEST;
import static com.digginroom.digginroom.TestFixture.나무;
import static com.digginroom.digginroom.TestFixture.블랙캣;
import static com.digginroom.digginroom.TestFixture.차이;
import static com.digginroom.digginroom.TestFixture.파워;
import static org.assertj.core.api.Assertions.assertThat;
package com.digginroom.digginroom.comment.controller;

import com.digginroom.digginroom.ControllerTest;
import com.digginroom.digginroom.TestFixture;
import com.digginroom.digginroom.domain.comment.Comment;
import com.digginroom.digginroom.domain.member.Member;
Expand All @@ -23,14 +15,24 @@
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import java.util.List;
import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.util.List;
import java.util.stream.Collectors;

import static com.digginroom.digginroom.TestFixture.COMMENT_UPDATE_REQUEST;
import static com.digginroom.digginroom.TestFixture.MEMBER2_LOGIN_REQUEST;
import static com.digginroom.digginroom.TestFixture.MEMBER_LOGIN_REQUEST;
import static com.digginroom.digginroom.TestFixture.나무;
import static com.digginroom.digginroom.TestFixture.블랙캣;
import static com.digginroom.digginroom.TestFixture.차이;
import static com.digginroom.digginroom.TestFixture.파워;
import static org.assertj.core.api.Assertions.assertThat;

@SuppressWarnings("NonAsciiCharacters")
class CommentControllerTest extends ControllerTest {

Expand All @@ -45,10 +47,8 @@ class CommentControllerTest extends ControllerTest {
private Room 차이;
private Member 파워;

@Override
@BeforeEach
void setUp() {
super.setUp();
파워 = memberRepository.save(파워());
memberRepository.save(블랙캣());
나무 = roomRepository.save(나무());
Expand All @@ -57,7 +57,7 @@ void setUp() {

@Test
void 댓글을_작성_할_수_있다() {
String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -75,7 +75,7 @@ void setUp() {

@Test
void 댓글은_500자_이하여야한다() {
String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -88,7 +88,7 @@ void setUp() {

@Test
void 댓글은_1자_이상이여야한다() {
String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -101,9 +101,8 @@ void setUp() {

@Test
void 유저는_자신의_댓글을_삭제할_수_있다() {
String cookie = login(MEMBER_LOGIN_REQUEST);

CommentResponse commentResponse = commentRequest(cookie, "댓글");
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);
CommentResponse commentResponse = commentWith(new CommentRequest("댓글"), cookie);

RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -115,10 +114,10 @@ void setUp() {

@Test
void 유저는_다른_유저의_댓글을_삭제할_수_없다() {
String powerCookie = login(MEMBER_LOGIN_REQUEST);
String blackcatCookie = login(MEMBER2_LOGIN_REQUEST);
String powerCookie = getCookieWith(MEMBER_LOGIN_REQUEST);
String blackcatCookie = getCookieWith(MEMBER2_LOGIN_REQUEST);

CommentResponse commentResponse = commentRequest(blackcatCookie, "댓글");
CommentResponse commentResponse = commentWith(new CommentRequest("댓글"), blackcatCookie);

RestAssured.given().log().all()
.cookie(powerCookie)
Expand All @@ -130,7 +129,7 @@ void setUp() {

@Test
void 유저는_존재하지_않은_댓글을_삭제할_수_없다() {
String powerCookie = login(MEMBER_LOGIN_REQUEST);
String powerCookie = getCookieWith(MEMBER_LOGIN_REQUEST);

RestAssured.given().log().all()
.cookie(powerCookie)
Expand All @@ -146,7 +145,7 @@ void setUp() {
commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워));
}

String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

List<String> comments = RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -171,7 +170,7 @@ void setUp() {
commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워));
}

String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

List<String> comments = RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -194,7 +193,7 @@ void setUp() {
commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워));
}

String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

List<String> comments = RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -217,7 +216,7 @@ void setUp() {
commentRepository.save(new Comment(나무.getId(), "댓글" + i, 파워));
}

String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

List<String> comments = RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -234,30 +233,10 @@ void setUp() {
assertThat(comments).containsExactly("댓글6", "댓글5");
}

private static String login(final MemberLoginRequest loginRequest) {
Response response = RestAssured.given().log().all()
.body(loginRequest)
.contentType(ContentType.JSON)
.when()
.post("/login");

return response.header("Set-Cookie");
}

private CommentResponse commentRequest(final String cookie, final String comment) {
return RestAssured.given().log().all()
.cookie(cookie)
.contentType(ContentType.JSON)
.body(new CommentRequest(comment))
.when().post("/rooms/" + 나무.getId() + "/comments").then().log()
.all().statusCode(HttpStatus.CREATED.value())
.extract().as(CommentResponse.class);
}

@Test
void 유저는_자신의_댓글을_수정할_수_있다() {
Comment comment = commentRepository.save(new Comment(나무.getId(), "댓글1", 파워));
String cookie = login(MEMBER_LOGIN_REQUEST);
String cookie = getCookieWith(MEMBER_LOGIN_REQUEST);

RestAssured.given().log().all()
.cookie(cookie)
Expand All @@ -271,4 +250,24 @@ private CommentResponse commentRequest(final String cookie, final String comment
Comment updatedComment = commentRepository.getCommentById(comment.getId());
assertThat(updatedComment.getComment()).isEqualTo(COMMENT_UPDATE_REQUEST.comment());
}

private String getCookieWith(final MemberLoginRequest loginRequest) {
Response response = RestAssured.given().log().all()
.body(loginRequest)
.contentType(ContentType.JSON)
.when()
.post("/login");

return response.header("Set-Cookie");
}

private CommentResponse commentWith(final CommentRequest comment, final String cookie) {
return RestAssured.given().log().all()
.cookie(cookie)
.contentType(ContentType.JSON)
.body(comment)
.when().post("/rooms/" + 나무.getId() + "/comments").then().log()
.all().statusCode(HttpStatus.CREATED.value())
.extract().as(CommentResponse.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.repository;
package com.digginroom.digginroom.comment.repository;

import static com.digginroom.digginroom.TestFixture.블랙캣;
import static com.digginroom.digginroom.TestFixture.파워;
Expand All @@ -9,27 +9,26 @@
import com.digginroom.digginroom.domain.comment.Comment;
import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.exception.CommentException;
import com.digginroom.digginroom.repository.CommentRepository;
import com.digginroom.digginroom.util.DatabaseCleanerExtension;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import java.util.List;
import java.util.stream.LongStream;
import org.junit.jupiter.api.BeforeEach;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.digginroom.digginroom.exception.CommentException;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;

@DataJpaTest
@Transactional
@ExtendWith(DatabaseCleanerExtension.class)
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class CommentRepositoryTest {

@PersistenceContext
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.service;
package com.digginroom.digginroom.comment.service;

import static com.digginroom.digginroom.TestFixture.블랙캣;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -10,6 +10,7 @@
import com.digginroom.digginroom.exception.CommentException.InvalidLastCommentIdException;
import com.digginroom.digginroom.repository.CommentRepository;
import com.digginroom.digginroom.repository.MemberRepository;
import com.digginroom.digginroom.service.CommentService;
import com.digginroom.digginroom.service.dto.CommentResponse;
import com.digginroom.digginroom.service.dto.CommentsResponse;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.exception;

import static org.hamcrest.Matchers.equalTo;

import com.digginroom.digginroom.ControllerTest;
import io.restassured.RestAssured;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static org.assertj.core.api.Assertions.assertThat;

import com.digginroom.digginroom.TestFixture;
import com.digginroom.digginroom.controller.ControllerTest;
import com.digginroom.digginroom.ControllerTest;
import com.digginroom.digginroom.feedback.domain.Feedback;
import com.digginroom.digginroom.feedback.dto.FeedbackRequest;
import com.digginroom.digginroom.feedback.dto.FeedbackResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.digginroom.digginroom.feedback;
package com.digginroom.digginroom.feedback.service;

import com.digginroom.digginroom.domain.member.Member;
import com.digginroom.digginroom.feedback.FeedbackService;
import com.digginroom.digginroom.feedback.domain.Feedback;
import com.digginroom.digginroom.feedback.dto.FeedbackRequest;
import com.digginroom.digginroom.feedback.dto.FeedbackResponse;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.member.controller;

import static org.hamcrest.Matchers.equalTo;

import com.digginroom.digginroom.ControllerTest;
import com.digginroom.digginroom.service.dto.ErrorResponse;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.member.controller;

import static org.hamcrest.Matchers.equalTo;

import com.digginroom.digginroom.TestFixture;
import com.digginroom.digginroom.ControllerTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.member.controller;

import com.digginroom.digginroom.service.dto.MemberLoginRequest;
import com.digginroom.digginroom.ControllerTest;
import com.digginroom.digginroom.repository.MemberRepository;
import com.digginroom.digginroom.service.dto.MemberLoginRequest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.hamcrest.Matchers;
Expand All @@ -12,19 +13,18 @@

import static com.digginroom.digginroom.TestFixture.MEMBER_LOGIN_REQUEST;
import static com.digginroom.digginroom.TestFixture.파워;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;

@SuppressWarnings("NonAsciiCharacters")
class MemberLoginControllerTest extends ControllerTest {

@Autowired
private MemberRepository memberRepository;


@Override
@BeforeEach
void setUp() {
super.setUp();
memberRepository.save(파워());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.digginroom.digginroom.controller;
package com.digginroom.digginroom.member.controller;

import static com.digginroom.digginroom.TestFixture.MEMBER_LOGIN_REQUEST;
import static com.digginroom.digginroom.domain.Genre.DANCE;
import static com.digginroom.digginroom.domain.Genre.RNB;
import static com.digginroom.digginroom.domain.Genre.ROCK;

import com.digginroom.digginroom.TestFixture;
import com.digginroom.digginroom.ControllerTest;
import com.digginroom.digginroom.service.dto.FavoriteGenresRequest;
import com.digginroom.digginroom.service.dto.MemberDetailsResponse;
import io.restassured.RestAssured;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.digginroom.digginroom.controller.dto;
package com.digginroom.digginroom.member.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down
Loading