由于www.example.com的原因,通过@SpringBootTest将TestContainers与Junit 5和依赖注入一起使用无法正常工作application.properties

cuxqih21  于 2022-10-30  发布在  Spring
关注(0)|答案(1)|浏览(257)

我收到以下错误:

  1. 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
  2. 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的Postgres。
我希望测试能够执行并且至少到达测试的开头。但是它从来没有到达测试并且抱怨我的application.properties。我使用带有正确的@Test注解的Junit 5。我还尝试了类似的方法,使用extends而不是implements作为https://www.baeldung.com/spring-boot-testcontainers-integration-test,我发现的例子都没有通过@Autowired注入一些东西,他们都使用Junit 4。我没有一个特殊的测试application.properties,只想让它使用默认的application.properties,但我无法通过@PropertySource(classpath...)链接它:
第一次
当我删除实现时,我得到了同样的错误,所以它最终可能与测试容器无关。这是使用测试容器的代码:
第一个

6rvt4ljy

6rvt4ljy1#

我不明白为什么要对PostgreSQLContainerInitializer使用interface,而且我也不认为JUnit5在这种情况下会执行@Testcontainers扩展。
最简单的方法可能是删除PostgreSQLContainerInitializer中的@Testcontainers@Container注解,而只在registerPgProperties()的开头调用postgres.start()

相关问题