question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integration test fails when executing "mvn test" in a web project with JPA that has a schema.sql file

See original GitHub issue

When 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:

  1. Clone the reference project from GitHub: https://github.com/matsev/springboot.sql.jpa.bug
  2. 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 the spring.jpa.hibernate.ddl-auto= is removed from the application.properties file
  • The test pass if executed from within IntelliJ
  • The test pass if @WebAppConfiguration is removed from the PersonControllerTest class.
  • Changing the value of spring.jpa.hibernate.ddl-auto to none 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:closed
  • Created 9 years ago
  • Comments:11 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
snicollcommented, Nov 6, 2020

@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.

0reactions
sunkuet02commented, Nov 6, 2020

I am facing this problem as of now. Is there any work around solutions ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found