java_demo/src/test/java/com/jenkins/demo/controller/HealthControllerTest.java

57 lines
2.3 KiB
Java
Raw Normal View History

package com.jenkins.demo.controller;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Health Controller Tests
*/
@ActiveProfiles("test")
@WebMvcTest(HealthController.class)
class HealthControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
@DisplayName("GET /api/health - 应该返回健康状态")
void shouldReturnHealthStatus() throws Exception {
mockMvc.perform(get("/api/health"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.status").value("UP"))
.andExpect(jsonPath("$.timestamp").exists())
.andExpect(jsonPath("$.application").value("Jenkins Demo Application"))
.andExpect(jsonPath("$.version").value("1.0.0"));
}
@Test
@DisplayName("GET /api/info - 应该返回应用信息")
void shouldReturnApplicationInfo() throws Exception {
mockMvc.perform(get("/api/info"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.name").value("Jenkins Demo Application"))
.andExpect(jsonPath("$.version").value("1.0.0"))
.andExpect(jsonPath("$.java.version").exists());
}
@Test
@DisplayName("GET /api/welcome - 应该返回欢迎信息")
void shouldReturnWelcomeMessage() throws Exception {
mockMvc.perform(get("/api/welcome"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.message").value("欢迎使用Jenkins Demo应用程序"))
.andExpect(jsonPath("$.description").exists());
}
}