JUnit5+TestContainers+@SpringBootTest集成测试:application.properties缺失
集成测试启动失败:无法找到application.properties
错误信息
org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [solutions.nabucco.nabuccobackendservice.user.core.repository.IntegrationTestRepository integrationTestRepository] in constructor [public solutions.nabucco.nabuccobackendservice.integration.IntegrationTest(solutions.nabucco.nabuccobackendservice.user.core.repository.IntegrationTestRepository,solutions.nabucco.nabuccobackendservice.user.core.service.jpa.UserService,solutions.nabucco.nabuccobackendservice.customer.core.service.jpa.CustomerService,...shortened output....)]: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [solutions.nabucco.nabuccobackendservice.Application]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/application.properties]
问题场景
我在做集成测试,目标是注入JPA服务/仓库,并通过TestContainers使用PostgreSQL。预期测试至少能执行到测试方法开头,但实际测试还未启动就抛出了上述application.properties相关的错误。
我使用JUnit5及正确的@Test注解,尝试过用继承而非实现接口的方式配置TestContainers,但在JUnit5下依然无效。找到的示例均基于JUnit4,没有涉及JUnit5的构造注入场景。我没有专门的测试用application.properties,只想使用默认配置,但无法通过@PropertySource(classpath...)关联配置文件。
移除implements关键字后错误依旧,推测问题可能与TestContainers无关。
相关代码
IntegrationTest类
@SpringBootTest @AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) public class IntegrationTest implements PostgreSQLContainerInitializer{ private final IntegrationTestRepository integrationTestRepository; private final UserService userService; private final CustomerService customerService; private final ShoppingCartService shoppingCartService; private final ProductCatalogService productCatalogService; private final SupplierService supplierService; private final FileStoreService fileStoreService; private final RoleService roleService; private final AuditLogService auditLogService; private final UserPreferencesRepository userPreferencesRepository; @Autowired public IntegrationTest( IntegrationTestRepository integrationTestRepository, UserService userService, CustomerService customerService, ShoppingCartService shoppingCartService, ProductCatalogService productCatalogService, SupplierService supplierService, FileStoreService fileStoreService, RoleService roleService, AuditLogService auditLogService, UserPreferencesRepository userPreferencesRepository) { this.integrationTestRepository = integrationTestRepository; this.userService = userService; this.customerService = customerService; this.shoppingCartService = shoppingCartService; this.productCatalogService = productCatalogService; this.supplierService = supplierService; this.fileStoreService = fileStoreService; this.roleService = roleService; this.auditLogService = auditLogService; this.userPreferencesRepository = userPreferencesRepository; } @Test @Transactional public void initDataPossible() { // 测试方法内容 } }
PostgreSQLContainerInitializer接口
@Testcontainers public interface PostgreSQLContainerInitializer { @Container PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:12.3"); @DynamicPropertySource static void registerPgProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.url", postgres::getJdbcUrl); registry.add("spring.datasource.username", postgres::getUsername); registry.add("spring.datasource.password", postgres::getPassword); } }
Application启动类
package solutions.nabucco.nabuccobackendservice; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client; @SpringBootApplication @EnableOAuth2Client public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
内容的提问来源于stack exchange,提问作者FireRedDev
相关产品推荐
相关产品推荐

