Fix post controller test setup

This commit is contained in:
Tim
2025-07-02 13:57:57 +08:00
parent 4f1cb92950
commit 62c18e6d5f
9 changed files with 65 additions and 12 deletions

View File

@@ -31,14 +31,18 @@ class CategoryControllerTest {
Category c = new Category();
c.setId(1L);
c.setName("tech");
Mockito.when(categoryService.createCategory(eq("tech"))).thenReturn(c);
c.setDescribe("d");
c.setIcon("i");
Mockito.when(categoryService.createCategory(eq("tech"), eq("d"), eq("i"))).thenReturn(c);
Mockito.when(categoryService.getCategory(1L)).thenReturn(c);
mockMvc.perform(post("/api/categories")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"tech\"}"))
.content("{\"name\":\"tech\",\"describe\":\"d\",\"icon\":\"i\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("tech"));
.andExpect(jsonPath("$.name").value("tech"))
.andExpect(jsonPath("$.describe").value("d"))
.andExpect(jsonPath("$.icon").value("i"));
mockMvc.perform(get("/api/categories/1"))
.andExpect(status().isOk())
@@ -50,10 +54,14 @@ class CategoryControllerTest {
Category c = new Category();
c.setId(2L);
c.setName("life");
c.setDescribe("d2");
c.setIcon("i2");
Mockito.when(categoryService.listCategories()).thenReturn(List.of(c));
mockMvc.perform(get("/api/categories"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("life"));
.andExpect(jsonPath("$[0].name").value("life"))
.andExpect(jsonPath("$[0].describe").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"));
}
}

View File

@@ -49,9 +49,13 @@ class PostControllerTest {
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
cat.setDescribe("d");
cat.setIcon("i");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
tag.setDescribe("td");
tag.setIcon("ti");
Post post = new Post();
post.setId(1L);
post.setTitle("t");
@@ -82,9 +86,13 @@ class PostControllerTest {
Category cat = new Category();
cat.setId(1L);
cat.setName("tech");
cat.setDescribe("d");
cat.setIcon("i");
Tag tag = new Tag();
tag.setId(1L);
tag.setName("java");
tag.setDescribe("td");
tag.setIcon("ti");
Post post = new Post();
post.setId(2L);
post.setTitle("hello");

View File

@@ -31,14 +31,18 @@ class TagControllerTest {
Tag t = new Tag();
t.setId(1L);
t.setName("java");
Mockito.when(tagService.createTag(eq("java"))).thenReturn(t);
t.setDescribe("d");
t.setIcon("i");
Mockito.when(tagService.createTag(eq("java"), eq("d"), eq("i"))).thenReturn(t);
Mockito.when(tagService.getTag(1L)).thenReturn(t);
mockMvc.perform(post("/api/tags")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"java\"}"))
.content("{\"name\":\"java\",\"describe\":\"d\",\"icon\":\"i\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("java"));
.andExpect(jsonPath("$.name").value("java"))
.andExpect(jsonPath("$.describe").value("d"))
.andExpect(jsonPath("$.icon").value("i"));
mockMvc.perform(get("/api/tags/1"))
.andExpect(status().isOk())
@@ -50,10 +54,15 @@ class TagControllerTest {
Tag t = new Tag();
t.setId(2L);
t.setName("spring");
t.setDescribe("d2");
t.setIcon("i2");
Mockito.when(tagService.listTags()).thenReturn(List.of(t));
mockMvc.perform(get("/api/tags"))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring"));
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring"))
.andExpect(jsonPath("$[0].describe").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"));
}
}