配置@DataJpaTest连接真实数据库遇加载上下文错误求解决
Spring Boot ORM模型包连接真实数据库测试失败
我需要针对真实数据库执行ORM模型测试,目前基于默认内存数据库能正常运行测试用例,但配置连接真实数据库时始终失败,报错如下:
java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: java.lang.IllegalStateException: Unable to retrieve @EnableAutoConfiguration base packages
我已经尝试了以下操作:
- 使用
@ContextConfiguration注解指定配置类 - 通过
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)禁用数据库自动配置 - 编写返回
DataSource类型的@Bean方法 - 尝试用
@ConfigurationProperties注解指定配置文件前缀 - 使用
@PropertySource注解引入配置文件
项目结构
这是一个供多项目复用的ORM模型包,并非独立Spring Boot应用,结构如下:
@Entity实体类路径:/src/main/java/com/myapp/models/@DataJpaTest测试类路径:/src/test/java/com/myapp/models/- 提供
DataSourceBean的配置类路径:/src/test/java/com/myapp/models/TestConfig.java - 测试配置文件路径:
src/test/resources/application-test.properties
相关代码示例
1. 实体类 /src/main/java/com/myapp/models/Country.java
@Entity @Table(name = "country", schema = "public") public class Country implements Serializable { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "country_country_id_seq") @SequenceGenerator(name = "country_country_id_seq", sequenceName = "country_country_id_seq", allocationSize = 1) @Column(name = "country_id", nullable = false) private Integer countryId; @Column(name = "country", nullable = false, length = 50) private String country; @Column(name = "last_update", nullable = false, columnDefinition = "TIMESTAMP DEFAULT now()") private Timestamp lastUpdate; // Getters and Setters }
2. 测试类 /src/test/java/com/myapp/models/CountryTest.java
@DataJpaTest @Transactional @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) @ContextConfiguration(classes = {TestConfig.class}) public class CountryTest { @Autowired private EntityManager entityManager; private Country country; @BeforeEach public void setUp() { country = new Country(); country.setCountry("Sample Country"); } @Test public void testPersistCountry() { entityManager.persist(country); entityManager.flush(); Country foundCountry = entityManager.find(Country.class, country.getCountryId()); assertNotNull(foundCountry); assertEquals(country.getCountry(), foundCountry.getCountry()); } // 更多测试方法 }
3. 测试配置类 /src/test/java/com/java/models/TestConfig.java
@Configuration @PropertySource("classpath:application-test.properties") public class TestConfig { @Bean @ConfigurationProperties(prefix = "test.datasource") public DataSource dataSource() { return DataSourceBuilder.create().build(); } }
4. 测试配置文件 /src/test/java/resources/application-test.properties
test.datasource.driver-class-name=org.postgresql.Driver test.datasource.url=jdbc:postgresql://localhost:5432/db_name test.datasource.username=db_user test.datasource.password=db_password spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
5. pom.xml配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.2</version> <relativePath/> </parent> <dependencies> <!-- Spring Boot Starter Data JPA for JPA and Hibernate --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Spring Boot Starter Test for testing --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Jakarta Persistence API --> <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.1.0</version> </dependency> <!-- H2 Database for in-memory testing --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <!-- PostgreSQL --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.7.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
内容的提问来源于stack exchange,提问作者CarlosAmds
相关产品推荐
相关产品推荐

