0
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class TaskProviderTest {
}

I'm using h2 database configured in property file

spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL
spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create-drop

And I'm using schema.sql and data.sql files for unit tests. Both scripts are executed on test startup.

Is there anyway to use different data.sql files for different Junit test cases ?

2
  • How are you loading data.sql currently ? I mean can you add the code ? Commented Nov 24, 2016 at 14:56
  • It is added automatically somehow by spring-boot Commented Nov 24, 2016 at 15:14

2 Answers 2

1

Try to use EmbeddedDataSource and configure it from method in code. The code below is demonstrates how to create datasource with 2 sql scripts

@Bean
public DataSource dataSource() {
    final EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();

    return builder
            .setType(EmbeddedDatabaseType.HSQL)
            .addScript("sql/create-db.sql")
            .addScript("sql/fill-db.sql")
            .build();
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Spring's EmbeddedDatabaseBuilder to construct the database in the setUp() method of your unit tests, providing different data.sql scripts for different tests.

Example:

public EmbeddedDatabase database(String dataScript) {
    return new EmbeddedDatabaseBuilder().
            setType(H2).
            addScript("schema.sql").
            addScript(dataScript).
            build();
}

Reference:

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseBuilder.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.