-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@ImportTestcontainers does not work with @DataJpaTest #39960
Comments
@ImportTestcontainers
cannot works with @DataJpaTest
Thanks for the report and sample project. Although not clear from the exception message, the underlying cause is that the I've opened spring-projects/spring-framework#32470 to see if the diagnostics can be improved on the Framework side. We'll also have to see what we can do on the Boot side to avoid the problem that triggers the exception in the first place. A workaround is to make the container package com.example.demo;
import static org.assertj.core.api.Assertions.assertThat;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.testcontainers.context.ImportTestcontainers;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration;
import org.springframework.context.weaving.LoadTimeWeaverAware;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
@DataJpaTest
@ImportAutoConfiguration(classes = ServiceConnectionAutoConfiguration.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@ImportTestcontainers
public class ImportContainersTests {
@Container
@ServiceConnection
static LoadTimeWeaverAwarePostgreSQLContainer<?> container = new LoadTimeWeaverAwarePostgreSQLContainer<>("postgres");
@Autowired
private DataSource dataSource;
@Test
void test() {
assertThat(dataSource).extracting("jdbcUrl").asString().startsWith("jdbc:postgresql://");
}
static class LoadTimeWeaverAwarePostgreSQLContainer<SELF extends LoadTimeWeaverAwarePostgreSQLContainer<SELF>> extends PostgreSQLContainer<SELF> implements LoadTimeWeaverAware {
LoadTimeWeaverAwarePostgreSQLContainer(String imageName) {
super(imageName);
}
@Override
public void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver) {
}
}
} This is horrible but it might indicate an avenue that's worth exploring for a proper fix. |
I suppose it would also help to let the |
It works fine with @Configuration
static class Config {
@Bean
@ServiceConnection
PostgreSQLContainer<?> container() {
return new PostgreSQLContainer<>("postgres");
}
} We should document current limitation and guide users to work around it. |
- upgrade to spring-boot 3.2.3; - upgrade to spring-cloud 2023.0.0; - disable UserStreamTest and UserEventRepositoryTest because ImportTestcontainers annotation is not working properly with Cassandra spring-projects/spring-boot#39960
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Is it fixed by 468e246? |
Yes. Thanks, @quaff. |
will throw exception:
BTW, It works fine with
@JdbcTest
.Here is reproducer importcontainers.zip
The text was updated successfully, but these errors were encountered: