import com.longxiang.backstage.controller.model.request.InsertMerchantReq;
import com.longxiang.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
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.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Author: Caster
* Date: 2022/9/14
* Comment:
*/
@Slf4j
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JUnitControllerTest {
@Autowired
private MockMvc mockMvc;
public HttpHeaders httpHeaders = new HttpHeaders();
@BeforeAll
static void beforeAll() throws Exception {
log.info("------ @BeforeAll ------");
}
@BeforeEach
void beforeEach() throws Exception {
log.info("------ @BeforeEach ------");
}
@AfterAll
static void afterAll() throws Exception {
log.info("------ @AfterAll ------");
}
@AfterEach
void afterEach() throws Exception {
log.info("------ @AfterEach ------");
}
@Test
@Order(0)
public void listTest() throws Exception {
MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
param.add("name", "1");
param.add("status", "1");
var result =
mockMvc.perform(
MockMvcRequestBuilders.get("/junit/list")
.headers(httpHeaders)
.params(param))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result);
}
@Test
@Order(1)
public void infoTest() throws Exception {
Integer id = 1;
var result =
mockMvc.perform(
MockMvcRequestBuilders.get("/junit/{id}", id)
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result);
}
@Test
@Order(2)
public void insertTest() throws Exception {
var insertInfo = new InsertMerchantReq();
var result =
mockMvc.perform(
MockMvcRequestBuilders.post("/junit")
.headers(httpHeaders)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(JsonUtil.toJson(insertInfo)))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result);
}
@Test
@Order(3)
public void updateTest() throws Exception {
Integer id = 1;
var insertInfo = new InsertMerchantReq();
var result =
mockMvc.perform(
MockMvcRequestBuilders.put("/junit/{id}", id)
.headers(httpHeaders)
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(JsonUtil.toJson(insertInfo)))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result);
}
@Test
@Order(4)
public void deleteTest() throws Exception {
Integer id = 1;
var result =
mockMvc.perform(
MockMvcRequestBuilders.delete("/junit/{id}", id)
.headers(httpHeaders))
.andExpect(status().isOk())
.andDo(print())
.andReturn();
System.out.println(result);
}
}