Because we are creating tests that do not need to access the Postgres database, we are migrating our tests to use H2 storage. We've created a separate application.properties in src/test/resources/application.properties overriding all values from our default src/main/resources/application.properties.
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.liquibase.enabled=false
spring.liquibase.change-log=
security.oidc_domain=123
security.jwt_key=123
api_url=http://localhost:8080
encryption.secret=123
security.debug=false
allowed_hosts=*
We have the following CoreApplication file which boots the Spring App:
@SpringBootApplication
@EnableScheduling
public class CoreApplication implements CommandLineRunner {
public static void main(final String[] args) {
SpringApplication application = new SpringApplication(CoreApplication.class);
application.run(args);
}
public ConfigurableApplicationContext context;
@Override
public void run(final String... args) throws Exception {
System.out.println(args);
context = SpringApplication.run(CoreApplication.class, args);
}
}
And this is our test file:
@SpringBootTest()
class CoreApplicationTests {
@Test()
void contextLoads() {
}
}
When we run this test (via IntelliJ IDEA) we get the following error:
Caused by: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.22.jar:5.3.22]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.22.jar:5.3.22]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
Before we added the custom application.properties in test is was working perfectly, but was hitting a connection limit to PostgreSQL because of the amount of tests, so it looks like this is caused by the application.properties override in tests folder.