Using H2 Memory Database to Test Applications with Oracle's 'SELECT sysdate FROM DUAL'

Using H2 Memory Database to Test Applications with Oracle's 'SELECT sysdate FROM DUAL'
Photo by Susan Wilkinson / Unsplash

Introduction:

When developing applications that rely on Oracle databases, it's common to use the sysdate function along with the DUAL table to retrieve the current date and time. However, when writing tests for these applications, you may encounter challenges due to the Oracle-specific syntax. In this post, we'll discuss how to use H2 memory database to test applications that use SELECT sysdate FROM DUAL.

Using H2 Memory Database for Testing:

H2 is an in-memory database that provides compatibility with various SQL databases, including Oracle. By configuring H2 to emulate Oracle's behavior, you can seamlessly test applications without modifying the code.

1. Set Up H2 Memory Database:

First, ensure H2 is added as a dependency in your project. Then, configure H2 in your application's application.properties or application.yml file:

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=RUNSCRIPT FROM 'classpath:init.sql'
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

In the above configuration:

  • spring.datasource.url: Specifies the H2 memory database URL with initialization script.
  • DB_CLOSE_DELAY=-1 and DB_CLOSE_ON_EXIT=FALSE: These properties ensure that the database remains open after the connection is closed, allowing access to the data during the entire test execution.
  • INIT=RUNSCRIPT FROM 'classpath:init.sql': This initializes the database by running the init.sql script located in the classpath.

2. Create DUAL-like Table and Initialize Data:

Create a init.sql file in the src/test/resources directory with the following content:

CREATE TABLE IF NOT EXISTS DUAL ( sysdate TIMESTAMP);

INSERT INTO DUAL (sysdate) VALUES (CURRENT_TIMESTAMP());

This script creates a DUAL-like table and inserts the current timestamp into it.

3. Modify the Test SQL Statement:

For testing, modify the SQL statement to insert or select the current date or timestamp from the DUAL-like table:

  • Inserting the current timestamp:

INSERT INTO DUAL (sysdate) VALUES (CURRENT_TIMESTAMP());

  • Selecting the current timestamp:

SELECT sysdate FROM DUAL;

Conclusion:

Using H2 memory database with a DUAL-like table allows you to test applications that rely on Oracle-specific syntax such as SELECT sysdate FROM DUAL. By configuring H2 to mimic Oracle's behavior, you can seamlessly test your applications without the need to modify the code.

Closing:

By following the steps outlined in this post, you can effectively use H2 memory database to test applications that use SELECT sysdate FROM DUAL, ensuring compatibility and reliability in your testing environment.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe