101 lines
3.5 KiB
Java
101 lines
3.5 KiB
Java
package com.emotion.controller;
|
|
|
|
import com.emotion.service.AuthService;
|
|
import org.junit.jupiter.api.Test;
|
|
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.test.web.servlet.MockMvc;
|
|
|
|
import static org.mockito.Mockito.when;
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
|
/**
|
|
* AuthController 测试类
|
|
*
|
|
* @author emotion-museum
|
|
* @date 2025-07-26
|
|
*/
|
|
@WebMvcTest(AuthController.class)
|
|
public class AuthControllerTest {
|
|
|
|
@Autowired
|
|
private MockMvc mockMvc;
|
|
|
|
@MockBean
|
|
private AuthService authService;
|
|
|
|
@Test
|
|
public void testCheckAccountExists() throws Exception {
|
|
// 模拟账户存在的情况
|
|
when(authService.existsByAccount("existingUser")).thenReturn(true);
|
|
|
|
mockMvc.perform(get("/auth/check-account")
|
|
.param("account", "existingUser"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(true));
|
|
}
|
|
|
|
@Test
|
|
public void testCheckAccountNotExists() throws Exception {
|
|
// 模拟账户不存在的情况
|
|
when(authService.existsByAccount("nonExistingUser")).thenReturn(false);
|
|
|
|
mockMvc.perform(get("/auth/check-account")
|
|
.param("account", "nonExistingUser"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(false));
|
|
}
|
|
|
|
@Test
|
|
public void testCheckEmailExists() throws Exception {
|
|
// 模拟邮箱存在的情况
|
|
when(authService.existsByEmail("existing@example.com")).thenReturn(true);
|
|
|
|
mockMvc.perform(get("/auth/check-email")
|
|
.param("email", "existing@example.com"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(true));
|
|
}
|
|
|
|
@Test
|
|
public void testCheckEmailNotExists() throws Exception {
|
|
// 模拟邮箱不存在的情况
|
|
when(authService.existsByEmail("nonexisting@example.com")).thenReturn(false);
|
|
|
|
mockMvc.perform(get("/auth/check-email")
|
|
.param("email", "nonexisting@example.com"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(false));
|
|
}
|
|
|
|
@Test
|
|
public void testCheckPhoneExists() throws Exception {
|
|
// 模拟手机号存在的情况
|
|
when(authService.existsByPhone("13800138000")).thenReturn(true);
|
|
|
|
mockMvc.perform(get("/auth/check-phone")
|
|
.param("phone", "13800138000"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(true));
|
|
}
|
|
|
|
@Test
|
|
public void testCheckPhoneNotExists() throws Exception {
|
|
// 模拟手机号不存在的情况
|
|
when(authService.existsByPhone("13900139000")).thenReturn(false);
|
|
|
|
mockMvc.perform(get("/auth/check-phone")
|
|
.param("phone", "13900139000"))
|
|
.andExpect(status().isOk())
|
|
.andExpect(jsonPath("$.code").value(200))
|
|
.andExpect(jsonPath("$.data").value(false));
|
|
}
|
|
}
|