feat : Signup Controller Test implement
This commit is contained in:
@@ -17,7 +17,8 @@ dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-jdbc:2.7.3'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-jdbc:2.7.3'
|
||||
implementation 'org.projectlombok:lombok:1.18.24'
|
||||
annotationProcessor 'org.projectlombok:lombok'
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.io.realworld.DTO;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
|
||||
import javax.validation.constraints.Email;
|
||||
@@ -10,6 +11,7 @@ import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
@JsonTypeName("user")
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
|
||||
public class UserSignupRequest {
|
||||
|
||||
@@ -11,6 +11,7 @@ import javax.validation.Valid;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserServiceImpl userService;
|
||||
|
||||
@@ -21,7 +22,7 @@ public class UserController {
|
||||
.email(user.getEmail())
|
||||
.bio(user.getBio())
|
||||
.image(user.getImage())
|
||||
.token(user.getToken()).build();
|
||||
.build();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,20 +20,21 @@ public class User {
|
||||
|
||||
private String bio;
|
||||
private String image;
|
||||
private String token;
|
||||
|
||||
@Builder
|
||||
public User(String username, String email, String password){
|
||||
public User(String username, String email, String password, String bio, String image) {
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.bio = bio;
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public static User of(String username, String email, String password){
|
||||
return new User(username,email,password);
|
||||
public static User of(String username, String email, String password) {
|
||||
return new User(username, email, password, "", "");
|
||||
}
|
||||
|
||||
protected User(){
|
||||
protected User() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.io.realworld.api.users;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.io.realworld.DTO.UserSignupRequest;
|
||||
import com.io.realworld.repository.User;
|
||||
import com.io.realworld.service.UserServiceImpl;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import org.springframework.test.web.servlet.ResultActions;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
|
||||
@WebMvcTest(controllers = UserController.class)
|
||||
class UserControllerTest {
|
||||
@Autowired
|
||||
MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@InjectMocks
|
||||
private UserController userController;
|
||||
|
||||
@MockBean
|
||||
private UserServiceImpl userService;
|
||||
|
||||
|
||||
@Test
|
||||
void signup() throws Exception {
|
||||
//given
|
||||
UserSignupRequest signupRequestTest = getUserSignupRequest();
|
||||
User signupResponseTest = getUserResponse();
|
||||
Mockito.doReturn(signupResponseTest).when(userService)
|
||||
.signup(any(UserSignupRequest.class));
|
||||
|
||||
//when
|
||||
ResultActions resultActions = mockMvc.perform(
|
||||
post("/users")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(signupRequestTest))
|
||||
);
|
||||
|
||||
|
||||
// then
|
||||
resultActions.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.user.email", signupResponseTest.getEmail()).exists())
|
||||
.andExpect(jsonPath("$.user.username", signupResponseTest.getUsername()).exists())
|
||||
.andExpect(jsonPath("$.user.bio", signupResponseTest.getBio()).exists())
|
||||
.andExpect(jsonPath("$.user.image", signupResponseTest.getImage()).exists());
|
||||
|
||||
|
||||
}
|
||||
|
||||
private UserSignupRequest getUserSignupRequest() {
|
||||
return UserSignupRequest.builder()
|
||||
.username("kms")
|
||||
.email("kms@gmail.com")
|
||||
.password("password")
|
||||
.build();
|
||||
}
|
||||
|
||||
private User getUserResponse() {
|
||||
return User.builder()
|
||||
.username("kms")
|
||||
.email("kms@gamil.com")
|
||||
.bio("1")
|
||||
.image("image")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.io.realworld.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UserServiceImplTest {
|
||||
|
||||
@Test
|
||||
void signup() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user