Initialization of ORACLE database In unit tests
NickName:Dogukan Evcil Ask DateTime:2021-06-24T21:50:22

Initialization of ORACLE database In unit tests

I have been trying to write some unit tests for my application. I am using oracle database in a docker container.

I am using ScriptUtils.executeSqlScript() to initialize the database from sql files. This work if I am using MySql database but I have PL/SQL in my Oracle schema and I get an exception when schemas executed (I am 100% sure that schemas work).

END; nested exception is java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

I guess ScriptUtils.executeSqlScript() does not support PL/SQL.

As I don't want to do volume binding in my docker-compose.yml I can't initialize tables when I start the docker container.

  1. Is there a better way of initializing tables for my JUnit tests that will work for any database technology?

  2. As you can see, I am generating tables before tests and dropping them after but I have other tests that will use those tables so I want to create tables once for all of the tests related to oracle. Is there any way of doing that?

@SpringBootTest
public class JdbcUserDaoImplTest {

    private static JdbcUserDaoImpl userDao;

    private User user;

    @BeforeClass
    public static void init() throws SQLException {
        userDao = new MysqlJdbcUserDaoImpl(dataSource());
        ScriptUtils.executeSqlScript(Objects.requireNonNull(userDao.getDataSource()).getConnection(), new ClassPathResource("pre.schema.sql"));
        ScriptUtils.executeSqlScript(userDao.getDataSource().getConnection(), new ClassPathResource("schema.sql"));
    }

    ... TEST CASES ...

    public static DataSource dataSource() {
        DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("oracle.jdbc.OracleDriver");
        dataSourceBuilder.url("jdbc:oracle:thin:@127.0.0.1:1507:XE");
        dataSourceBuilder.username("system");
        dataSourceBuilder.password("oracle");
        return dataSourceBuilder.build();
    }

    @AfterClass
    public static void destroy() throws SQLException {
        ScriptUtils.executeSqlScript(Objects.requireNonNull(userDao.getDataSource()).getConnection(), new ClassPathResource("drop.sql"));
    }
}

EDIT

Example of undeployable SQL

CREATE TRIGGER item_trigger
    BEFORE INSERT
    ON items
    FOR EACH ROW
BEGIN

    IF :NEW.item_date IS NULL
    THEN
        SELECT CURRENT_TIMESTAMP INTO :NEW.item_date FROM DUAL;
    END IF;

    IF :NEW.item_uid IS NULL
    THEN
        SELECT SYS_GUID() INTO :NEW.item_uid FROM DUAL;
    END IF;

END;

Copyright Notice:Content Author:「Dogukan Evcil」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68117079/initialization-of-oracle-database-in-unit-tests

More about “Initialization of ORACLE database In unit tests” related questions

Initialization of ORACLE database In unit tests

I have been trying to write some unit tests for my application. I am using oracle database in a docker container. I am using ScriptUtils.executeSqlScript() to initialize the database from sql files...

Show Detail

How to use Oracle as an embedded database for running unit tests in maven?

I have an application for which the prod/test/qa database is only Oracle. Furthermore, I suspect SQL queries to be Oracle-specific. And unfortunatly, this application has second to no unit tests. ...

Show Detail

Unit Tests - where to place initialization code?

I like to have unit tests which are small and just have the call to the function under test and Asserts. But I am confused about where to place the initialization code? There are a lot of variable...

Show Detail

Unit tests and database

This question about unit tests sparked another thing that's been bothering me. I've gone back and forth on three ways to do unit tests when hitting a database. Create mock objects and plug them in...

Show Detail

How to handle static initialization when running unit tests?

I have some code which runs fine, but I would also add unit tests for it too. And I have problem not with tests per se, but with static initialization (constructor and fields) -- they are not execu...

Show Detail

How to emulate Oracle (+) outer join notation in in-memory database used to run Java unit tests?

We are using HSQLDB with Oracle database syntax(jdbc:hsqldb:mem:TestDB;sql.syntax_ora=true) for our datalayer unit tests. (We realize this is not ideal and it would be better if we could test agai...

Show Detail

Fail the unit tests when the database schema, and entity are changed, but unit tests are not changed

Following is the exact scenario in my application: There are several C# methods in the codebase which are using Entity framework to talk with SQL database. Unit tests are written against all metho...

Show Detail

Speeding up integration tests that rely on an Oracle DB

We have an Oracle database server specifically for our unit tests to run against. Is there a way to tune Oracle specifically for this kind of purpose? As the data is constantly being thrown away ...

Show Detail

Should unit tests cover stress testing?

I am wondering if you guys have any good reading to consider what to classify as unit testing / acceptance / integration testing. I have the following scenario and we're having a bit of a debate at...

Show Detail

In Django 1.6 - how to use a pre-existing database for unit tests? (--keepdb flag)

We are using Django 1.6.x for our codebase and there are no unit tests for the code. I want to begin to implement unit tests on the codebase and we have a Oracle database we can use for the task. On

Show Detail