0

I get the following Errors:

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]

This is happening in an Integration Test that wants to use JPA Services/Repositories that are injected and a Postgres via Testcontainers.

I am expecting the test to execute and at least reach the beginning of the Test. Instead it never reaches the test and complains about my application.properties. I use Junit5 with the correct @Test Annotation. I also tried a similar approach using extends instead of implements as https://www.baeldung.com/spring-boot-testcontainers-integration-test , but that also was not working with Junit5. The examples i found all dont inject something via @Autowired and the new way of constructor injection in Junit5, they all use Junit4. I dont have a special test application.properties and just want it to use the default one, i wasnt able to link it via @PropertySource(classpath...):

enter image description here enter image description here

I get the same error when i remove implements, so it might not be related to the testcontainers in the end? This is the Code Using the TestContainers:

@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() {
@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);
  }
}

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);
  }
}
  • The problem appears to be related to `solutions.nabucco.nabuccobackendservice.Application` which you haven't shared. – Andy Wilkinson Oct 25 '22 at 15:58
  • added this code, but i think its irrelevant – FireRedDev Nov 02 '22 at 07:36
  • Have you actually **read** the stacktrace? `Could not open ServletContext resource [/application.properties]` seems pretty clear. For some reason it isn't loading your `application.properties` from the classpath but as a servlet root. So I suspect you have some configuration class that messes around with how things are loaded. – M. Deinum Nov 02 '22 at 07:41
  • I believe it is relevant as the error message says "Failed to parse configuration class [solutions.nabucco.nabuccobackendservice.Application]". I can see from that `Application` class that you're using `@EnableOAuth2Client` from https://github.com/spring-attic/spring-security-oauth. This project is no longer maintained and you should be using Spring Security's built-in OAuth2 support instead. [This guide](https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide) should help you to migrate. – Andy Wilkinson Nov 02 '22 at 10:10

1 Answers1

0

I don't understand why you use an interface for PostgreSQLContainerInitializer and I also don't think that JUnit5 will execute the @Testcontainers extension in this case.

The easiest approach would be probably to drop the @Testcontainers and @Container annotations in PostgreSQLContainerInitializer and just call postgres.start() at the beginning of registerPgProperties().

Kevin Wittek
  • 1,369
  • 9
  • 26
  • I dont really understand why that would be related to the error? I tried it with the traditional approach like baldeung before, that didnt work either, same error – FireRedDev Nov 02 '22 at 08:31
  • There is nothing traditional about the approach in the Baeldung article. Did you follow my recommendation? Besides, as others have pointed out, the error message seems to indicate an error completely independent from Testcontainers. – Kevin Wittek Nov 02 '22 at 17:35