在正确基点上继续开发
Change-Id: Id4ef3e3bd2627176ea8254561140a7bf570f9cdb
diff --git a/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
new file mode 100644
index 0000000..61d6aff
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
@@ -0,0 +1,104 @@
+// src/test/java/com/ptp/ptplatform/controller/HelpPostControllerTest.java
+package com.ptp.ptplatform.controller;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ptp.ptplatform.entity.HelpComment;
+import com.ptp.ptplatform.entity.HelpPost;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+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.web.servlet.MockMvc;
+
+import java.util.List;
+
+import static org.hamcrest.Matchers.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+@SpringBootTest
+@AutoConfigureMockMvc
+public class HelpPostControllerTest {
+
+ @Autowired MockMvc mockMvc;
+ @Autowired ObjectMapper objectMapper;
+
+ private int postId;
+
+ @BeforeEach
+ void setup() throws Exception {
+ // 1. 创建一个帖子
+ HelpPost post = new HelpPost();
+ post.setAuthorId(1001);
+ post.setTitle("测试帮助帖");
+ post.setContent("帮助区内容示例");
+ String body = objectMapper.writeValueAsString(post);
+
+ // 2. 调接口,拿到完整的 JSON
+ String json = mockMvc.perform(post("/help/posts")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(body))
+ .andExpect(status().isOk())
+ .andReturn().getResponse().getContentAsString();
+
+ // 3. 解析 JSON,取到 data.post.id
+ JsonNode node = objectMapper.readTree(json);
+ postId = node.path("data").path("post").path("id").asInt();
+ }
+
+ @Test
+ void testHelpPostLifecycle() throws Exception {
+ // —— 列表分页 ——
+ mockMvc.perform(get("/help/posts")
+ .param("page", "1")
+ .param("size", "5"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.records[0].id", is(postId)));
+
+ // —— 详情(此时无评论) ——
+ mockMvc.perform(get("/help/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.post.id", is(postId)))
+ .andExpect(jsonPath("$.data.comments", hasSize(0)));
+
+ // —— 点赞帖子 ——
+ mockMvc.perform(post("/help/posts/{id}/like", postId))
+ .andExpect(status().isOk());
+
+ mockMvc.perform(get("/help/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.post.likeCount", is(1)));
+
+ // —— 发布评论 ——
+ HelpComment comment = new HelpComment();
+ comment.setAuthorId(2002);
+ comment.setContent("测试帮助评论");
+ String cbody = objectMapper.writeValueAsString(comment);
+
+ String cres = mockMvc.perform(post("/help/posts/{id}/comments", postId)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(cbody))
+ .andExpect(status().isOk())
+ .andReturn().getResponse().getContentAsString();
+
+ // 解析 commentId
+ JsonNode cnode = objectMapper.readTree(cres);
+ int commentId = cnode.path("data").path("commentId").asInt();
+
+ // 列详情确认评论出现
+ mockMvc.perform(get("/help/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.comments[0].id", is(commentId)));
+
+ // —— 评论点赞 ——
+ mockMvc.perform(post("/help/comments/{id}/like", commentId))
+ .andExpect(status().isOk());
+
+ mockMvc.perform(get("/help/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.comments[0].likeCount", is(1)));
+ }
+}
diff --git a/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java b/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java
new file mode 100644
index 0000000..95c431f
--- /dev/null
+++ b/src/test/java/com/ptp/ptplatform/controller/SeedPostControllerTest.java
@@ -0,0 +1,105 @@
+package com.ptp.ptplatform.controller;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.ptp.ptplatform.entity.SeedComment;
+import com.ptp.ptplatform.entity.SeedPost;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+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.web.servlet.MockMvc;
+
+import static org.hamcrest.Matchers.*;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+
+@SpringBootTest
+@AutoConfigureMockMvc
+public class SeedPostControllerTest {
+
+ @Autowired
+ private MockMvc mockMvc;
+
+ @Autowired
+ private ObjectMapper objectMapper;
+
+ private int postId;
+
+ @BeforeEach
+ void setup() throws Exception {
+ SeedPost post = new SeedPost();
+ post.setAuthorId(3003);
+ post.setTitle("测试求种帖");
+ post.setContent("求种区内容示例");
+ String json = objectMapper.writeValueAsString(post);
+
+ String result = mockMvc.perform(post("/seed/posts")
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(json))
+ .andExpect(status().isOk())
+ .andReturn()
+ .getResponse()
+ .getContentAsString();
+
+ // 从 Result.data.post.id 里取出刚创建的 postId
+ JsonNode node = objectMapper.readTree(result);
+ postId = node.path("data").path("post").path("id").asInt();
+ }
+
+ @Test
+ void testSeedPostLifecycle() throws Exception {
+ // 列表分页
+ mockMvc.perform(get("/seed/posts")
+ .param("page", "1")
+ .param("size", "5"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.records[0].id", is(postId)));
+
+ // 详情(无评论)
+ mockMvc.perform(get("/seed/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.post.id", is(postId)))
+ .andExpect(jsonPath("$.data.comments", hasSize(0)));
+
+ // 点赞
+ mockMvc.perform(post("/seed/posts/{id}/like", postId))
+ .andExpect(status().isOk());
+
+ mockMvc.perform(get("/seed/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.post.likeCount", is(1)));
+
+ // 发布评论
+ SeedComment comment = new SeedComment();
+ comment.setAuthorId(4004);
+ comment.setContent("测试求种评论");
+ String cjson = objectMapper.writeValueAsString(comment);
+
+ String cres = mockMvc.perform(post("/seed/posts/{id}/comments", postId)
+ .contentType(MediaType.APPLICATION_JSON)
+ .content(cjson))
+ .andExpect(status().isOk())
+ .andReturn()
+ .getResponse()
+ .getContentAsString();
+
+ // 从 Result.data.commentId 中取出 commentId
+ JsonNode cnode = objectMapper.readTree(cres);
+ int commentId = cnode.path("data").path("commentId").asInt();
+
+ mockMvc.perform(get("/seed/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.comments[0].id", is(commentId)));
+
+ // 点赞评论
+ mockMvc.perform(post("/seed/comments/{id}/like", commentId))
+ .andExpect(status().isOk());
+
+ mockMvc.perform(get("/seed/posts/{id}", postId))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$.data.comments[0].likeCount", is(1)));
+ }
+}
diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml
index 45122fa..45b607b 100644
--- a/src/test/resources/application-test.yml
+++ b/src/test/resources/application-test.yml
@@ -2,7 +2,14 @@
spring:
application:
name: PTPlatform-test # 区分测试环境
-
+
+ spring:
+ sql:
+ init:
+ mode: always # 一定要始终执行,不论你用的是哪个 profile
+ schema-locations: classpath:schema.sql
+
+
# 数据源配置 (H2 内存数据库)
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MYSQL # 模拟MySQL语法
@@ -10,7 +17,6 @@
username: sa
password:
-
# JPA/Hibernate 配置
jpa:
hibernate:
diff --git a/src/test/resources/schema.sql b/src/test/resources/schema.sql
new file mode 100644
index 0000000..677d41c
--- /dev/null
+++ b/src/test/resources/schema.sql
@@ -0,0 +1,40 @@
+CREATE TABLE IF NOT EXISTS help_posts (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ author_id INT NOT NULL,
+ title VARCHAR(255) NOT NULL,
+ content TEXT NOT NULL,
+ like_count INT NOT NULL DEFAULT 0,
+ reply_count INT NOT NULL DEFAULT 0,
+ create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()
+);
+
+
+CREATE TABLE IF NOT EXISTS help_comments (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ post_id INT NOT NULL,
+ author_id INT NOT NULL,
+ content TEXT NOT NULL,
+ like_count INT NOT NULL DEFAULT 0,
+ create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
+ CONSTRAINT fk_help_comments_post FOREIGN KEY (post_id) REFERENCES help_posts(id) ON DELETE CASCADE
+);
+
+CREATE TABLE IF NOT EXISTS seed_posts (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ author_id INT NOT NULL,
+ title VARCHAR(255) NOT NULL,
+ content TEXT NOT NULL,
+ like_count INT NOT NULL DEFAULT 0,
+ reply_count INT NOT NULL DEFAULT 0,
+ create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP()
+);
+
+CREATE TABLE IF NOT EXISTS seed_comments (
+ id INT AUTO_INCREMENT PRIMARY KEY,
+ post_id INT NOT NULL,
+ author_id INT NOT NULL,
+ content TEXT NOT NULL,
+ like_count INT NOT NULL DEFAULT 0,
+ create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP(),
+ CONSTRAINT fk_seed_comments_post FOREIGN KEY (post_id) REFERENCES seed_posts(id) ON DELETE CASCADE
+);