Spring Boot 集成测试中的 IllegalStateException

问题描述 投票:0回答:1

我在为 Spring Boot 应用程序运行集成测试时遇到

IllegalStateException
。该错误似乎与测试期间尝试连接到 PostgreSQL 的 Flyway 有关,即使我在
application-test.properties
文件中禁用了 Flyway。

@ActiveProfiles("test")
@SpringBootTest(classes = SampleApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
@ExtendWith(SpringExtension.class) 
public abstract class AbstractSpringIntegrationTest{      
@LocalServerPort     
public int port;  
}
public class SampleControllerTest extends AbstractSpringIntegrationTest {     
@MockBean     
private SampleService sampleService;      
@Autowired     
private ObjectMapper objectMapper;      
@Test     
void testMethodSuccess() {       ......     }

下面是我为测试用例创建的测试/资源/application-test.properties。

# Disable Flyway for tests
spring.flyway.enabled=false

运行测试用例时,我收到以下错误消息

SampleControllerTest > testMethodSuccess() FAILED
    java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:180
        Caused by: org.flywaydb.core.internal.exception.FlywaySqlException at JdbcUtils.java:60
            Caused by: org.postgresql.util.PSQLException at ConnectionFactoryImpl.java:346
                Caused by: java.net.ConnectException at Net.java:-2

我在这里缺少什么?

即使在

application-test.properties
中禁用了 Flyway,Flyway 似乎仍在尝试执行迁移并连接到 PostgreSQL,从而导致
ConnectException

我尝试过的:

  1. 验证

    spring.flyway.enabled=false
    设置正确。

  2. 检查测试期间是否有任何其他配置可能触发 Flyway 或 PostgreSQL 连接。

如何解决此问题,以便我的集成测试能够在不尝试连接到 PostgreSQL 且 Flyway 不尝试执行迁移的情况下运行?

spring-boot unit-testing integration illegalargumentexception
1个回答
0
投票

您可能需要检查一些事项:

  1. 飞路版本
  2. 数据库类型和版本
  3. 指定您正在使用的操作系统。

这应该可以解决。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.