/
MemberControllerTest.java
129 lines (104 loc) · 4.58 KB
/
MemberControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.book.member;
import com.book.member.domain.Email;
import com.book.member.domain.Member;
import com.book.member.domain.Name;
import com.book.member.domain.Password;
import com.book.member.dto.MemberSignupRequest;
import com.book.member.service.MemberHelperService;
import com.book.member.service.MemberSignUpService;
import com.book.test.BaseControllerTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.RunWith;
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.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@WebMvcTest(MemberController.class)
public class MemberControllerTest extends BaseControllerTest {
@Autowired
private WebApplicationContext context;
@MockBean
private MemberHelperService memberHelperService;
@MockBean
private MemberSignUpService memberSignUpService;
private final String TEST_PASSWORD = "password";
private final String TEST_FIRST_NAME = "test";
private final String TEST_LAST_NAME = "test";
private final String TEST_EMAIL = "test@test.com";
private MockMvc mvc;
@Before
public void setUp() {
mvc = buildMockMvc(context);
}
@Test
@DisplayName("이메일양식이 유효하지 않습니다.")
public void signupIsFail() throws Exception {
//given
final MemberSignupRequest request = buildSignupRequest("ansatgol", "password", "test", "test");
//when
mvc.perform(
post("/members")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andDo(print())
.andExpect(status().isBadRequest());
//then
}
@Test
@DisplayName("회원가입성공")
public void SignupIsSuccess() throws Exception {
//given
final MemberSignupRequest request = buildSignupRequest(TEST_EMAIL, TEST_PASSWORD, TEST_FIRST_NAME, TEST_LAST_NAME);
given(memberSignUpService.signUp(any())).willReturn(request.toMember());
//when
final String memberAsString = mvc.perform(
post("/members")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andDo(print())
.andExpect(status().isCreated())
.andReturn()
.getResponse()
.getContentAsString();
final Member member = objectMapper.readValue(memberAsString, Member.class);
//then
Assert.assertThat(request.getEmail(), equalTo(member.getEmail()));
Assert.assertThat(request.getName(), equalTo(member.getName()));
}
@Test
public void getUser_GetMember_Member() throws Exception {
//given
final Member mockMember = buildMember(TEST_EMAIL, TEST_PASSWORD, TEST_FIRST_NAME, TEST_LAST_NAME);
given(memberHelperService.findById(mockMember.getId())).willReturn(mockMember);
//when
mvc.perform(
get("/members/{id}", 1))
.andDo(print())
.andExpect(status().isOk());
//then
}
private MemberSignupRequest buildSignupRequest(final String email, final String password, final String firstname, final String lastname) {
return MemberSignupRequest.builder()
.email(new Email(email))
.password(new Password(password))
.name(new Name(firstname, lastname))
.build();
}
private Member buildMember(final String email, final String password, final String firstname, final String lastname) {
return new Member(new Email(email), new Password(password), new Name(firstname, lastname));
}
}