fix: fix Spring Boot test configuration issues

This commit is contained in:
wangtianqi 2025-06-23 21:26:08 +08:00
parent 7b8bd55554
commit 22d7087d1f
4 changed files with 9 additions and 1 deletions

View File

@ -2,12 +2,14 @@ package com.jenkins.demo;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
/** /**
* Jenkins Demo Application Tests * Jenkins Demo Application Tests
*/ */
@SpringBootTest @SpringBootTest
@ActiveProfiles("test")
@TestPropertySource(locations = "classpath:application-test.properties") @TestPropertySource(locations = "classpath:application-test.properties")
class JenkinsDemoApplicationTests { class JenkinsDemoApplicationTests {

View File

@ -5,6 +5,7 @@ import org.junit.jupiter.api.DisplayName;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@ -13,6 +14,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
/** /**
* Health Controller Tests * Health Controller Tests
*/ */
@ActiveProfiles("test")
@WebMvcTest(HealthController.class) @WebMvcTest(HealthController.class)
class HealthControllerTest { class HealthControllerTest {

View File

@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.util.Arrays; import java.util.Arrays;
@ -25,6 +26,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* User Controller Integration Tests * User Controller Integration Tests
*/ */
@WebMvcTest(UserController.class) @WebMvcTest(UserController.class)
@ActiveProfiles("test")
class UserControllerTest { class UserControllerTest {
@Autowired @Autowired

View File

@ -1,4 +1,6 @@
# Test Configuration # Test Configuration
spring.profiles.active=test # 注意不要在profile-specific文件中设置spring.profiles.active
logging.level.com.jenkins.demo=DEBUG logging.level.com.jenkins.demo=DEBUG
server.port=0 server.port=0
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=false