Skip to content
Advertisement

Hibernate JPA Integration Test issue

ISSUE

I’m trying to do some integration testing on my REST controller in Spring. I’ve set up a BEFORE_TEST sql script to run to create a table along with some default values before each test, but they don’t seem to be inserted as expected.

Here is my integration test setup…

JavaScript

Here are my two SQL scripts…

Schema…

JavaScript

Data…

JavaScript

Here is my specimen class values and constructor…

JavaScript

Here’s the part of the controller I’m testing…

JavaScript

TEST RESULT

Test result is saying id expected was 2, but it got 1. It should be 2, as the SQL scripts should be creating an instance of specimen in the DB and auto incrementing the id to 1, then my test should be creating a second, with an id of 2. However, the SQL script doesn’t seem to be inserting the initial DB entry.

JavaScript

Side note – I’ve not used dates much in Java, so could it be something to do with the date field? It was working fine when I tested in Postman, I’ve set the test date entry to null as a placeholder as I’m unsure of the java date syntax.

Thanks in advance.

Advertisement

Answer

You specified the same script name specimen-schema.sql twice:

JavaScript

The sql script to insert the first record was never run.

specimen-data.sql :

JavaScript

So the record new Specimen("Tyrannosaurus_Rex", "California, USA", "Container_A", null, "Jaw Bone"); has been saved with the id 1.

You should change the second "classpath:specimen-schema.sql" to "classpath:specimen-data.sql"

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement