package com.heima.article.controller.v1;
import com.heima.model.article.dtos.ArticleInfoDto;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
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.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@SuppressWarnings(“all”)
@SpringBootTest
@RunWith(SpringRunner.class)
//使用MocMvc进行测试 SpringBoot提供的
@AutoConfigureMockMvc
public class ArticleInfoControllerTest {
@Autowired
MockMvc mvc;
@Autowired
ObjectMapper mapper;
@Testpublic void testLoadArticleInfo() throws Exception { ArticleInfoDto dto = new ArticleInfoDto(); dto.setArticleId(10120);//1为数据库中的测试数据 MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/api/v1/article/load_article_info") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(mapper.writevalueAsBytes(dto));//String.valueOf(dto)与此用mapper的方法有啥不同 mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());}
}
f