I have this situation with my Spring Boot application and the management of tests:
The application is running correctly. But for its unit tests, regular tests are OK, while integration tests are giving me some issues.
In order to be able to operate with a database for the tests, I do the following in a base class from which all integration tests inherit:
public abstract class IntegrationTestBase {
protected static PostgreSQLContainer<?> postgres = null;
private static boolean databaseStarted = false;
@BeforeAll
public static void beforeAll() {
if (!databaseStarted) {
postgres = new PostgreSQLContainer<>("postgres:16-alpine")
.withReuse(true);
postgres.start();
databaseStarted = true;
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// Ideally this should stop the DB only after executing all classes
postgres.stop();
}));
}
}
}
I can launch a test individually through a command like this:
mvn test -Dtest="UserServiceIT"
But when I try to launch all of them:
mvn failsafe:integration-test
I get the following error:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
Invocation of init method failed; nested exception is javax.persistence.PersistenceException:
[PersistenceUnit: default] Unable to build Hibernate SessionFactory;
nested exception is org.hibernate.tool.schema.spi.SchemaManagementException:
Schema-validation: wrong column type encountered in column [item_id] in table [Item];
found [int8 (Types#BIGINT)], but expecting [int4 (Types#INTEGER)]
The class "Item" comes from an external jar and therefore I am unable to change its type, but I can tell you it is defined as a long.
I have been trying to make workarounds such as trying to redefine the entityManagerFactory but that only makes things more complicated. After all the tests are working when executing individually.
Do you know what could be causing this? Am I launching the tests the right way?
@SpringBootTestor@DataJpaTest? Example for the latter (replace MySQLContainer with PostgreSQL): github.com/roar-skinderviken/spring-jpa-auditing-demo/blob/…withReusewill keep your container running after test is finished, can it be the case where a previous run was with an int type instead of long?withReusestatement, but the result is the same. The issue happens from the first test.