Skip to content

Commit

Permalink
Merge pull request #7 from kkijuk/feature/2
Browse files Browse the repository at this point in the history
[넬리] 마스터 자기소개서 테스트 코드 작성
  • Loading branch information
nanna29 authored Jul 22, 2024
2 parents 1a5f3df + 61d29d8 commit 019c7cb
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import umc.kkijuk.server.introduce.domain.MasterIntroduce;
import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto;
import umc.kkijuk.server.introduce.response.BaseErrorResponse;
import umc.kkijuk.server.introduce.response.BaseException;
import umc.kkijuk.server.introduce.error.BaseErrorResponse;
import umc.kkijuk.server.introduce.error.BaseException;
import umc.kkijuk.server.introduce.service.MasterIntroduceService;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.kkijuk.server.introduce.response;
package umc.kkijuk.server.introduce.error;
import lombok.Getter;

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.kkijuk.server.introduce.response;
package umc.kkijuk.server.introduce.error;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.kkijuk.server.introduce.response;
package umc.kkijuk.server.introduce.error;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository;
import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto;
import umc.kkijuk.server.introduce.dto.MasterIntroduceResDto;
import umc.kkijuk.server.introduce.response.BaseException;
import umc.kkijuk.server.introduce.error.BaseException;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package umc.kkijuk.server.introduce.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.DisplayName;
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.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import umc.kkijuk.server.introduce.domain.MasterIntroduce;
import umc.kkijuk.server.introduce.domain.MasterIntroduceRepository;
import umc.kkijuk.server.introduce.dto.MasterIntroduceReqDto;
import umc.kkijuk.server.introduce.service.MasterIntroduceService;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Transactional
class MasterIntroduceControllerTest {
@LocalServerPort
private int port;
@Autowired
private MasterIntroduceRepository masterIntroduceRepository;
@Autowired
private MockMvc mockMvc;
@Autowired
private MasterIntroduceService masterIntroduceService;
@Autowired
private ObjectMapper objectMapper;

@Test
@DisplayName("마스터 자기소개서 생성 테스트")
public void postMaster() throws Exception{
final String oneLiner= "one-liner-test";
final String subTitle= "sub-title-test";
final String content= "content-test";

MasterIntroduceReqDto masterIntroduceReqDto= MasterIntroduceReqDto.builder()
.oneLiner(oneLiner)
.subTitle(subTitle)
.content(content)
.build();


mockMvc.perform(post("/history/intro/master")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(masterIntroduceReqDto)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.oneLiner").value("one-liner-test"))
.andExpect(jsonPath("$.data.subTitle").value("sub-title-test"))
.andExpect(jsonPath("$.data.content").value("content-test"));
}

@Test
@DisplayName("마스터 자기소개서 수정 테스트")
public void updateMaster() throws Exception {
final String oneLiner = "one-liner-test";
final String subTitle = "sub-title-test";
final String content = "content-test";

MasterIntroduce masterIntroduce = masterIntroduceRepository.save(MasterIntroduce.builder()
.oneLiner(oneLiner)
.subTitle(subTitle)
.content(content)
.build());

Long id = masterIntroduce.getId(); // Retrieve the ID of the saved entity

String expectedOneLiner = "one-liner2";
String expectedSubTitle = "sub-title2";
String expectedContent = "content2";

MasterIntroduceReqDto masterIntroduceReqDto = MasterIntroduceReqDto.builder()
.oneLiner(expectedOneLiner)
.subTitle(expectedSubTitle)
.content(expectedContent)
.build();

mockMvc.perform(patch("/history/intro/master")
.param("id", id.toString())
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(masterIntroduceReqDto)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.oneLiner").value(expectedOneLiner))
.andExpect(jsonPath("$.data.subTitle").value(expectedSubTitle))
.andExpect(jsonPath("$.data.content").value(expectedContent));
}
}
5 changes: 2 additions & 3 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ spring.application.name=server
# spring data source
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/kkijuk?characterEncoding=UTF-8&serverTimezone=Asia/Seoul
spring.datasource.username=test
spring.datasource.password=testPW
spring.datasource.username=root
spring.datasource.password=zheldelqlWkd29

# spring jpa
spring.jpa.database=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true


commit.hash=local

0 comments on commit 019c7cb

Please sign in to comment.