test: 为 AdminController 添加单元测试

backend/dev
Spark 2 months ago
parent 8b4c9dfff1
commit f18cb5917b

@ -2,9 +2,11 @@ package cc.aspark.domain.dto;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class UserLoginDTO {
@Min(3)

@ -2,9 +2,11 @@ package cc.aspark.domain.dto;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class UserRegisterDTO {
@Min(3)
@Max(20)

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -15,6 +16,7 @@ import java.time.LocalDateTime;
* @TableName tb_admin
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Tag(name = "管理员实体类")

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@ -15,6 +16,7 @@ import java.time.LocalDateTime;
* @TableName tb_stu
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Tag(name = "学生实体类")

@ -1,8 +1,10 @@
package cc.aspark.domain.vo;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class UserLoginVO {
private Integer id;

@ -0,0 +1,87 @@
package cc.aspark.controller;
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;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import cc.aspark.domain.dto.UserLoginDTO;
import cc.aspark.domain.dto.UserRegisterDTO;
import cc.aspark.domain.vo.UserLoginVO;
import cc.aspark.service.AdminService;
import net.bytebuddy.implementation.bytecode.Throw;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
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.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@SpringBootTest
@AutoConfigureMockMvc
public class AdminControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private AdminService adminService;
@BeforeEach
public void setup() {
UserLoginDTO userLoginDTO = UserLoginDTO.builder()
.username("test")
.password("abcd1234")
.build();
UserRegisterDTO userRegisterDTO = UserRegisterDTO.builder()
.username("test")
.password("abcd1234")
.build();
UserLoginVO userLoginVO = UserLoginVO.builder()
.id(1)
.username("test")
.token("test")
.build();
Mockito.when(adminService.login(userLoginDTO)).thenReturn(userLoginVO);
Mockito.doNothing().when(adminService).register(userRegisterDTO);
}
@Test
public void testLogin() throws Exception {
ResultActions resultActions = mockMvc.perform(post("/admin/login")
.contentType("application/json")
.content("{\"username\": \"test\", \"password\": \"abcd1234\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1))
.andExpect(jsonPath("$.msg").value("success"))
.andExpect(jsonPath("$.data.id").value(1))
.andExpect(jsonPath("$.data.username").value("test"))
.andExpect(jsonPath("$.data.token").value("test"))
.andDo(print());
}
@Test
public void testRegister() throws Exception {
ResultActions resultActions = mockMvc.perform(post("/admin/register")
.contentType("application/json")
.content("{\"username\": \"test\", \"password\": \"abcd1234\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(1))
.andExpect(jsonPath("$.msg").value("success"))
.andDo(print());
}
}
Loading…
Cancel
Save