调整ArticleController的测试类

This commit is contained in:
禾几海
2020-07-24 00:22:40 +08:00
parent b7f26cbfdb
commit 7be1c9dfb0

View File

@@ -9,10 +9,10 @@ import cn.celess.blog.entity.model.PageData;
import cn.celess.blog.entity.request.ArticleReq; import cn.celess.blog.entity.request.ArticleReq;
import cn.celess.blog.mapper.ArticleMapper; import cn.celess.blog.mapper.ArticleMapper;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@@ -31,7 +31,7 @@ public class ArticleControllerTest extends BaseTest {
public void create() { public void create() {
ArticleReq articleReq = new ArticleReq(); ArticleReq articleReq = new ArticleReq();
// 应该正常通过 // 应该正常通过
articleReq.setTitle("test-" + UUID.randomUUID().toString()); articleReq.setTitle("test-" + randomStr());
articleReq.setMdContent("# test title"); articleReq.setMdContent("# test title");
articleReq.setCategory("随笔"); articleReq.setCategory("随笔");
String[] tagList = {"tag", "category"}; String[] tagList = {"tag", "category"};
@@ -39,58 +39,30 @@ public class ArticleControllerTest extends BaseTest {
articleReq.setOpen(true); articleReq.setOpen(true);
articleReq.setType(true); articleReq.setType(true);
articleReq.setUrl("http://xxxx.com"); articleReq.setUrl("http://xxxx.com");
JSONObject jsonObject = JSONObject.fromObject(articleReq); MockHttpServletRequestBuilder post = post("/admin/article/create");
try { try {
// 未登录 getMockData(post, adminLogin(), articleReq).andDo(result -> {
mockMvc.perform(post("/admin/article/create") Response<ArticleModel> response = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<Response<ArticleModel>>() {
.content(jsonObject.toString()) });
.contentType("application/json")) assertEquals(SUCCESS.getCode(), response.getCode());
.andExpect(status().isOk()) assertNotNull(response.getResult());
.andDo(result -> { ArticleModel articleModel = response.getResult();
assertEquals(HAVE_NOT_LOG_IN.getCode(), assertNotNull(articleModel.getId());
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code) assertNotNull(articleModel.getTitle());
); assertNotNull(articleModel.getSummary());
}); assertNotNull(articleModel.getOriginal());
// User权限 assertNotNull(articleModel.getTags());
String token = userLogin(); assertNotNull(articleModel.getCategory());
mockMvc.perform(post("/admin/article/create") assertNotNull(articleModel.getPublishDateFormat());
.content(jsonObject.toString()) assertNotNull(articleModel.getMdContent());
.contentType("application/json") assertNotNull(articleModel.getPreArticle());
.header("Authorization", token)) assertNull(articleModel.getNextArticle());
.andExpect(status().isOk()) assertNotNull(articleModel.getOpen());
.andDo(result -> { assertNotNull(articleModel.getReadingNumber());
assertEquals(PERMISSION_ERROR.getCode(), assertNotNull(articleModel.getAuthor());
JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code) assertNotNull(articleModel.getUrl());
); });
});
// Admin权限
token = adminLogin();
mockMvc.perform(post("/admin/article/create")
.content(jsonObject.toString())
.contentType("application/json")
.header("Authorization", token))
.andExpect(status().isOk())
.andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());
assertEquals(SUCCESS.getCode(), object.getInt(Code));
ArticleModel articleModel = (ArticleModel) JSONObject.toBean(object.getJSONObject(Result), ArticleModel.class);
assertNotNull(articleModel.getId());
assertNotNull(articleModel.getTitle());
assertNotNull(articleModel.getSummary());
assertNotNull(articleModel.getOriginal());
assertNotNull(articleModel.getTags());
assertNotNull(articleModel.getCategory());
assertNotNull(articleModel.getPublishDateFormat());
assertNotNull(articleModel.getMdContent());
assertNotNull(articleModel.getPreArticle());
assertNull(articleModel.getNextArticle());
assertNotNull(articleModel.getOpen());
assertNotNull(articleModel.getReadingNumber());
assertNotNull(articleModel.getAuthor());
assertNotNull(articleModel.getUrl());
});
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }