Skip to content

Commit

Permalink
Test : 테스트코드 오류 수정 및 BeforeEach 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhun0331 committed Jul 29, 2024
1 parent cd1bd24 commit 37f75ea
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package umc.kkijuk.server.member.dto;


import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -11,9 +12,11 @@
@Data
@NoArgsConstructor
public class MemberInfoChangeDto {
private Long id; //jwt토큰으로 받을땐 제거
@NotNull
private String phoneNumber;
@NotNull
private LocalDate birthDate;
@NotNull
private MarketingAgree marketingAgree;

@Builder
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/umc/kkijuk/server/member/dto/MemberJoinDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package umc.kkijuk.server.member.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand All @@ -13,13 +14,21 @@
@NoArgsConstructor
public class MemberJoinDto {

@NotNull
private String email;
@NotNull
private String name;
@NotNull
private String phoneNumber;
@NotNull
private LocalDate birthDate;
@NotNull
private String password;
@NotNull
private String passwordConfirm;
@NotNull
private MarketingAgree marketingAgree;
@NotNull
private State userState;

// @Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package umc.kkijuk.server.member.dto;

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class MemberPhoneNumberDto {
@NotNull
private String phoneNumber;

@Builder
Expand Down
146 changes: 0 additions & 146 deletions src/test/java/umc/kkijuk/server/member/service/MemberInfoTest.java

This file was deleted.

101 changes: 100 additions & 1 deletion src/test/java/umc/kkijuk/server/member/service/MemberServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package umc.kkijuk.server.member.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.member.controller.response.MemberFieldResponse;
import umc.kkijuk.server.member.controller.response.MemberInfoResponse;
import umc.kkijuk.server.member.controller.response.ResultResponse;
import umc.kkijuk.server.member.domain.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.member.domain.MarketingAgree;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.State;
import umc.kkijuk.server.member.dto.MemberFieldDto;
import umc.kkijuk.server.member.dto.MemberInfoChangeDto;
import umc.kkijuk.server.member.dto.MemberJoinDto;
import umc.kkijuk.server.member.repository.MemberJpaRepository;

import java.time.LocalDate;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
public class MemberServiceTest {

@Autowired
Expand All @@ -36,6 +43,15 @@ public class MemberServiceTest {
@Autowired
private TestRestTemplate restTemplate;

@BeforeEach
public void setUp() {
Member member = new Member("[email protected]", "홍길동",
"01012345678", LocalDate.parse("1999-03-31"),
"testpassword", MarketingAgree.BOTH, State.ACTIVATE);
member.changeFieldInfo(List.of("game", "computer"));
memberJpaRepository.save(member);
}

private String testMemberEmail = "[email protected]";
@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
Expand Down Expand Up @@ -115,4 +131,87 @@ public void joinExceptionWIthPasswordIncorrect() {
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(response.getBody()).contains("Passwords do not match");
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 내정보_조회_성공() {
// Given

// When
ResponseEntity<MemberInfoResponse> response = restTemplate.getForEntity("/member/myPage/info", MemberInfoResponse.class);

// Then
assertEquals(HttpStatus.OK, response.getStatusCode());

MemberInfoResponse body = response.getBody();
assertNotNull(body);
assertEquals("[email protected]", body.getEmail());
assertEquals("홍길동", body.getName());
assertEquals("01012345678", body.getPhoneNumber());
assertEquals(LocalDate.parse("1999-03-31"), body.getBirthDate());
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 내정보_수정() {
// Given

MemberInfoChangeDto changeDto = MemberInfoChangeDto.builder()
.phoneNumber("010-5678-1234")
.birthDate(LocalDate.parse("2000-01-01"))
.marketingAgree(MarketingAgree.EMAIL)
.build();

HttpEntity<MemberInfoChangeDto> requestEntity = new HttpEntity<>(changeDto);

// When
ResponseEntity<Boolean> response = restTemplate.exchange("/member/myPage/info", HttpMethod.PUT, requestEntity, Boolean.class);

// Then
assertEquals(HttpStatus.OK, response.getStatusCode());
Boolean body = response.getBody();
assertNotNull(body);
assertEquals(true, body);

// Verify changes
Member updatedMember = memberJpaRepository.findById(1L).orElse(null);
assertNotNull(updatedMember);
assertEquals("010-5678-1234", updatedMember.getPhoneNumber());
assertEquals(LocalDate.parse("2000-01-01"), updatedMember.getBirthDate());
assertEquals(MarketingAgree.EMAIL, updatedMember.getMarketingAgree());
}

@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 관심분야_조회(){
// 회원추가

ResponseEntity<MemberFieldResponse> response = restTemplate.getForEntity("/member/myPage/field", MemberFieldResponse.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
MemberFieldResponse body = response.getBody();
assertNotNull(body);
assertEquals(List.of("game", "computer"), body.getField());
}


@Test
@DirtiesContext(methodMode = DirtiesContext.MethodMode.AFTER_METHOD)
public void 관심분야_수정(){
//given

MemberFieldDto memberFieldDto = MemberFieldDto.builder().field(List.of("book", "movie")).build();
HttpEntity<MemberFieldDto> request = new HttpEntity<>(memberFieldDto);
//when
ResponseEntity<Boolean> response = restTemplate.postForEntity("/member/myPage/field", request, Boolean.class);
//then
assertEquals(HttpStatus.OK, response.getStatusCode());
Boolean body = response.getBody();
assertNotNull(body);
assertEquals(true, body);

Member updatedMember = memberJpaRepository.findById(1L).orElse(null);
assertNotNull(updatedMember);
assertEquals(List.of("book", "movie"), updatedMember.getField());
}
}

0 comments on commit 37f75ea

Please sign in to comment.