Post ์์ฒญ ํ ์คํธ ์ฝ๋ ์์ฑ
Post ์์ฒญ์ผ๋ก ์ฌ์ฉ์๋ฅผ ๋ฑ๋กํ๋ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํด๋ณด๋ฉฐ Junit 5 ํ ์คํธ ์ฝ๋๋ฅผ ์์๋ณด์.
JPA๋ฅผ ์ด์ฉํด MySQL์ User๋ฅผ ๋ฑ๋กํ๋ ์ฝ๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ๋ค.
model/User.java
package com.vividswan.studymate.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import javax.persistence.*;
import java.sql.Timestamp;
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, length = 100, unique = true)
private String username;
@Column(nullable = false, length = 100)
private String nickname;
@Column(nullable = false, length = 50)
private String email;
@Column(nullable = false, length = 100)
private String password;
@Enumerated(EnumType.STRING)
private RoleType role;
@CreationTimestamp
private Timestamp createDate;
}
controller/userController
package com.vividswan.studymate.controller;
import com.vividswan.studymate.dto.UserJoinDto;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class UserController {
@GetMapping("join")
public String joinForm(){
return "user/joinForm";
}
}
service/UserService
package com.vividswan.studymate.service;
import com.vividswan.studymate.dto.UserJoinDto;
import com.vividswan.studymate.model.User;
import com.vividswan.studymate.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
@Transactional
public void join(UserJoinDto userJoinDto) {
userRepository.save(userJoinDto.toEntity());
}
}
repositoey/UserRepository
package com.vividswan.studymate.repository;
import com.vividswan.studymate.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
}
์์ ๊ฐ์ model, controller, service, repository๋ฅผ ์์ฑํด์ค๋ค.
๊ทธ ๋ค์ ํด๋ผ์ด์ธํธ๋จ์์ ์ ์ ์ ๋ณด๋ฅผ ๋ณด๋ด์ค js ํ์ผ๊ณผ ์คํ๋ง์์์ api controller๋ฅผ ์์ฑํด์ค๋ค.
static/js/user.js
let index ={
init : function() {
$("#btn-join").on("click",()=>{
this.save();
});
},
save : function(){
let data = {
username:$("#username").val(),
password:$("#password").val(),
email:$("#email").val(),
nickname:$("#nickname").val(),
};
$.ajax({
type:"POST",
url:"/api/joinProc",
data:JSON.stringify(data),
contentType:"application/json; charset=utf-8"
})
.done(function(response){
if(response.status === 500){
alert("๋ก๊ทธ์ธ์ ์คํจํ์์ต๋๋ค.");
}
else{
alert("๋ก๊ทธ์ธ์ ์ฑ๊ณตํ์์ต๋๋ค.");
}
location.href="/";
}).fail(function (error){
alert(JSON.stringify(error));
});
}
}
index.init();
api/UserApiController
package com.vividswan.studymate.api;
import com.vividswan.studymate.dto.UserJoinDto;
import com.vividswan.studymate.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequiredArgsConstructor
public class UserApiController {
private final UserService userService;
@PostMapping("/api/joinProc")
public int joinProc(@RequestBody UserJoinDto userJoinDto){
userService.join(userJoinDto);
return HttpStatus.OK.value();
}
}
์์ UserApiController
์ ๋ํ ํ
์คํธ ์ผ์ด์ค๋ฅผ ์์ฑํด๋ณด์.
UserApiContollerTest.java
package com.vividswan.studymate.api;
import com.vividswan.studymate.dto.UserJoinDto;
import com.vividswan.studymate.model.User;
import com.vividswan.studymate.repository.UserRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
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.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserApiControllerTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private UserRepository userRepository;
@AfterEach
public void tearDown() throws Exception{
userRepository.deleteAll();
}
@Test
public void joinTest() throws Exception{
//given
String username = "vividswan";
String password = "1234";
String email = "vividswan@naver.com";
String nickname = "soohwan";
UserJoinDto userJoinDto = UserJoinDto.builder()
.username(username)
.password(password)
.email(email)
.nickname(nickname)
.build();
String url = "http://localhost:"+port+"/api/joinProc";
//when
ResponseEntity<Long> responseEntity = restTemplate.postForEntity(url,userJoinDto,Long.class);
//then
Assertions.assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
Assertions.assertThat(responseEntity.getBody()).isGreaterThan(0L);
List<User> all = userRepository.findAll();
Assertions.assertThat(all.get(0).getUsername()).isEqualTo(username);
Assertions.assertThat(all.get(0).getEmail()).isEqualTo(email);
}
}
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
์คํ๋ง๋ถํธ๋ก ๋์ํ๋ ํ ์คํธ์์ ์ ์ธํ๊ณ , ๋๋คํ ํฌํธ๋ฅผ ์ฌ์ฉํ ์ ์๋ ์ด๋ ธํ ์ด์ ์ด๋ค.
@Autowired
private TestRestTemplate restTemplate;
ResponseEntity
๋ก ํฌ์คํธ ์์ฒญ์ ํ๊ธฐ ์ํด TestRestTemplate๋ฅผ ์ด์ฉํ๋ค.
@AfterEach
public void tearDown() throws Exception{
userRepository.deleteAll();
}
๊ฐ ํ
์คํธ๊ฐ ๋๋๋ฉด repository
๋ฅผ ์ด๊ธฐํ ํ ์ ์๊ฒ @AfterEach๋ก ์ ์ธํ ๋ค ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ง์์ฃผ๋๋ก ํ๋ค.
//when
ResponseEntity responseEntity = restTemplate.postForEntity(url,userJoinDto,Long.class);
TestRestTemplate๋ก ๋๋ค ํฌํธ์ url์ dto๋ฅผ ๋ณด๋ด์ฃผ๊ณ Long.class์ ๋ฐํ๊ฐ์ ๋ฐ๋๋ค.
Assertions.assertThat
Assertions.assertThat
์ผ๋ก ์ฐ๊ฒฐ์ํ์ ์๋ฃ์ ์ผ์น ์ฌ๋ถ๋ฅผ ํ์ธํ๋ค.
์ด ๋ Assertions๋ Junit์ด ์๋ assertj
๋ก ์ํดํธํ์ฌ ์ฌ์ฉํ๋ค.
import org.assertj.core.api.Assertions;
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring GlobalExceptionHandler ์์ฑ (0) | 2022.03.13 |
---|---|
mustache์์ summernote ์ฌ์ฉ (0) | 2022.03.13 |
์น ์๋ฒ์ ํฐ์ผ์ ์ฐจ์ด (0) | 2022.03.11 |
Spring์ Security-UserDetails ๊ตฌํ (0) | 2022.03.11 |
devtools ์ค์ (intellij) (0) | 2022.03.11 |