parent
8b4c9dfff1
commit
f18cb5917b
@ -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…
Reference in new issue