Spring Boot — print Hibernate SQL statements and parameters during integration tests without editing config files

IntelliJ JUnit test config to use ENV vars and enable hibernate logging

Elvis Ciotti
2 min readFeb 14, 2022

Override of application properties from ENV vars

Spring boot supports override of application properties from environment variables.

As explained in the related section (v2.4.5), you basically just need to add an ENV var with the same name of the application property, upper case and with dot replaced with an underscore (as the majority of OS do not support dot as a variable name)

.. the configuration property spring.main.log-startup-info would be an environment variable named SPRING_MAIN_LOGSTARTUPINFO.

Settings to print hibernate statements

As also explained here, the settings to log the queries are

# application.properties
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

How to configure IntelliJ to print queries during integration tests

From the Run/Debug configuration. choose ‘Edit configuration templates…”, then in “Environment variables”. add the following

LOGGING_LEVEL_ORG_HIBERNATE=DEBUG;LOGGING_LEVEL_ORG_HIBERNATE_TYPE_DESCRIPTOR_SQL=TRACE
IntelliJ Run/Debug configuration
IntelliJ JUnit configuration

IMPORTANT: note that I voluntarily didn’t add the class name (last part of the setting) to the env variables. This is because of Spring internal mechanism, further details are given here.

Queries and parameters are now printed in the console.

--

--

Elvis Ciotti
Elvis Ciotti

Written by Elvis Ciotti

Software Contractor — Java, Spring, k8s, AWS, Javascript @ London - hire me at https://elvisciotti.github.io/

No responses yet