Search code examples
javaspring-booth2spring-test

How do I ensure my @Sql queries are shown in my console log?


Here's an MRE:

package com.example.h2_demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.jdbc.Sql;

@DataJpaTest
@Sql(
        executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
        scripts = {"/create_user_table.sql", "/insert_user.sql"}
)
public class H2DemoApplicationTest {
    @Test
    void testNothing() {
    }

    @Test
    void testNothingOneMoreTime() {
    }
}
-- create_user_table.sql

CREATE TABLE IF NOT EXISTS users(
  id INTEGER PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  last_name VARCHAR(50)
);
-- insert_user.sql

INSERT INTO users
VALUES (1, 'John', 'Doe');

I tinkered with the scripts a bit and now am fairly sure that the @Sql scripts are executed. However, it's not obvious when you look in the logs. No relevant CREATE or INSERT statements are present

I did try every property I saw suggested on the Internet (including on StackOverflow). At this point, my properties are:

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.hibernate.type=trace

logging.level.org.springframework.jdbc.core=TRACE
logging.level.org.springframework.test.context.jdbc=debug

I double-checked and made sure that these values were present in the application.properties of the target folder

No logs still (sorry for the image, don't see a workaround)

text search for "insert" provides no matches

How do I see those @Sql queries in my logs?


Solution

  • If you want to see which @Sql scripts are being executed, set the org.springframework.test.context.jdbc logging category to DEBUG.

    If you want to see which SQL statements are being executed, set the org.springframework.jdbc.datasource.init logging category to DEBUG.

    Note that I've also opened a ticket to improve the documentation in Spring to reflect this.