简体   繁体   中英

Spring boot unit test with h2 failing for schema

I have sql file with insert statements like below

insert into demo.course (col1, col2) VALUES( 'Val 1', 'KS1')

My unit test

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase //h2
@ActiveProfiles("test")
class TopicRepositoryTest extends BaseRepositoryTest {

    @Test
    public void newTopicCreationShouldBePossible() {        
        Topics newTopic = createTopics("STEAM"); //from base
        assertNotNull(newTopic.getId());
    }
}

Application-test.props

spring.datasource.url=jdbc:h2:mem:testdb;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false

spring.jpa.properties.hibernate.default_schema=DEMO

spring.datasource.hikari.schema=DEMO

Error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of class path resource [maria/importcommon.sql]: insert into demo.mytable (col1, col2) VALUES( 'Val 1', 'KS1'); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "DEMO" not found; SQL statement:

Reading some posts, I added the below, but it is not working

spring.jpa.properties.hibernate.default_schema=DEMO

spring.datasource.hikari.schema=DEMO

How can I fix this issue and pass my tests?

I suggest to update your insert statements without schema prefix

insert into course (col1, col2) VALUES( 'Val 1', 'KS1');

removed "demo."

You can create schema if not exists using spring.datasource.url

spring.datasource.url=jdbc:h2:mem:testdb;INIT=CREATE SCHEMA IF NOT EXISTS DEMO;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM