Integration test fails when executing "mvn test" in a web project with JPA that has a schema.sql file
See original GitHub issueWhen having a web-based project with a JPA based repository, @WebAppConfiguration
integration test fails when executing mvn clean test
when there is a schema.sql
file present , despite setting spring.jpa.hibernate.ddl-auto=
as described in chapter 63.3 Initialize a database using Spring JDBC:
If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to “” (preferable) or “none”.
Step by step:
- Clone the reference project from GitHub: https://github.com/matsev/springboot.sql.jpa.bug
- Execute
mvn clean test
Expected result:
- All tests should pass
Actual result:
BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'
[...]
BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource;
[...]
BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Initialization of bean failed;
[...]
BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed;
[...]
ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource URL [[...]/target/classes/schema.sql]: CREATE TABLE person ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name VARCHAR(255) ); nested exception is java.sql.SQLSyntaxErrorException: object name already exists: PERSON in statement [CREATE TABLE person ( id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, name VARCHAR(255) )]
Comments:
- The test pass the
schema.sql
file is deleted and thespring.jpa.hibernate.ddl-auto=
is removed from theapplication.properties
file - The test pass if executed from within IntelliJ
- The test pass if
@WebAppConfiguration
is removed from thePersonControllerTest
class. - Changing the value of
spring.jpa.hibernate.ddl-auto
tonone
does not prevent the test from failing
Environment:
- Spring Boot 1.1.5.RELEASE
- Maven 3.2.3
- Java 1.8.0_20
Issue Analytics
- State:
- Created 9 years ago
- Comments:11 (9 by maintainers)
Top Results From Across the Web
IntegrationTest isolation fails in springboot 2.2.2.RELEASE ...
I was getting the same error after upgrading to Spring Boot 2.2.1 from 2.2.0. It looks 2.2.1 has updated the h2 dependency to...
Read more >Testing JPA Queries with Spring Boot and @DataJpaTest
An in-depth tutorial about Spring Boot's support for testing JPA ... executing a custom schema.sql file when the application starts up.
Read more >14. Integration Testing - Spring
A JdbcTemplate , for executing SQL statements to query the database. Such queries can be used to confirm database state both prior to...
Read more >Configuring Separate Spring DataSource for Tests - Baeldung
A quick, practical tutorial on how to configure a separate data source for testing in a Spring application.
Read more >Unit test reports - GitLab Docs
First, GitLab Runner uploads all JUnit report format XML files as artifacts to ... The Test summary panel shows how many tests failed,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sunkuet02 this issue is four years old so I am not sure what exact problem you’re facing. If you think you’ve found a bug in Spring Boot, please create a new issue with a small sample we can run ourselves to reproduce the problem.
I am facing this problem as of now. Is there any work around solutions ?