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...):
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);
}
}