-
Notifications
You must be signed in to change notification settings - Fork 872
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
Testcontainers does not configure the test with matching containers #1247
Comments
This comment was marked as resolved.
This comment was marked as resolved.
It would be beneficial to separate the configuration from the application. That's what is generated. @TestConfiguration(proxyBeanMethods = false)
public class TestDemoApplication {
@Bean
@ServiceConnection
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
}
public static void main(String[] args) {
SpringApplication.from(DemoApplication::main).with(TestDemoApplication.class).run(args);
}
} But it would be better to get how the example in the reference documentation looks like (https://docs.spring.io/spring-boot/docs/3.2.1/reference/htmlsingle/#features.testcontainers.at-development-time) For example: @TestConfiguration(proxyBeanMethods = false)
public class TestDemoConfiguration {
@Bean
@ServiceConnection
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
}
}
@Import(TestDemoConfiguration.class)
public class TestDemoApplication {
public static void main(String[] args) {
SpringApplication.from(DemoApplication::main).with(TestDemoConfiguration.class).run(args);
}
} Because especially for new Spring Boot developers it may look strange to import an application class in test classes. |
I am not following the argument, nor why the example you've shared doesn't "import an application class". I am also assuming that your example having two classes with the same name is a mistake on your end. |
I'm sorry @snicoll but, I thought it might be clear from the issue description. The issue describes that the generated Test doesn't work because it has no database connection:
To overcome this problem you can import the application class:
My comment is that it looks silly to import an Application and therefore it would be better to separate configuration and application. |
I'd say that a way to make it clear is to share an example of what you actually mean, rather than relying on context. That last snippet is much better IMO.
OK but we're not even there yet. We'll revisit this based on whatever we decide doing. |
This comment was marked as off-topic.
This comment was marked as off-topic.
I and others have run into this issue: it's unexpected to have a test fail "out of the box". While a better solution is being worked on, documenting this behavior somewhere would save a lot of troubleshooting effort. Perhaps this could be a comment in the generated test class? |
Being on |
My suggestion would be: generate a dedicated test configuration class for the testcontainers: @TestConfiguration(proxyBeanMethods = false)
class TestcontainersConfiguration {
@Bean
@ServiceConnection
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
}
} The class which holds the test- public class TestSt1247Application {
public static void main(String[] args) {
SpringApplication.from(St1247Application::main).with(TestcontainersConfiguration.class).run(args);
}
} and the test class has to be augmented with an @SpringBootTest
@Import(TestcontainersConfiguration.class)
class St1247ApplicationTests {
@Test
void contextLoads() {
}
} With that, we don't import the "application" class, |
I could successfully run the tests with the following setup. The test class itself:
The containers configuration:
For the repository tests I used a base class even if it was not needed:
|
Using spring initializr with following configuration
https://start.spring.io/#!type=maven-project&language=java&platformVersion=3.1.1&packaging=jar&jvmVersion=17&groupId=com.demo&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.demo.demo&dependencies=lombok,devtools,docker-compose,web,data-jpa,validation,flyway,postgresql,testcontainers,restdocs
Following error occurs during the provided contextLoads() test
This issue can be solved by importing the example TestConfiguration in the test class.
Should this be done by default?
The text was updated successfully, but these errors were encountered: