Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check in Play Framework if a unit test performed a database insert/update/delete

I've got a base class that all my controller tests extend. This is set up so each unit test can have its own Fixture to load from.

So the @Before override will check what the required fixture for the test is, load it and then start the test.

The problem here is that this reloads the fixture every single time. Even if the method only did a select on the database, altering nothing.

The internal memory database the Play Framework uses is the H2 database. I wonder if there is a way to check after a unit test is done, if there has been a change to the database and if not, skip the reloading of the exact same data.

I tried identity_scope, it returns null no matter what.

like image 557
KdgDev Avatar asked Aug 12 '15 17:08

KdgDev


People also ask

Should unit tests touch the database or anything out of process?

It is meant to make sure that definable modules of code work as expected. To test an application it is not enough to use unit tests. You must also perform functional testing and regression testing. Database access falls outside the scope of unit testing, so you would not write unit tests that include database access.

What is database unit testing?

Database Unit testing requires that the tester should have knowledge in checking tables, writing queries and writing procedures, so to make tests effectively. Testing can be performed in a web application or desktop because the database can be used in the application.


1 Answers

H2 has no possiblity of checking when were last changes applied. However You could just make an additional column that saves last modification time with

CREATE TABLE TEST(ID INT, NAME VARCHAR, LAST_MOD TIMESTAMP AS NOW());

That way You could save a time of test start, and then query the DB if there are any modification dates after the test start time.

I don't see any other possible way

like image 186
Krzysztof Wende Avatar answered Nov 15 '22 07:11

Krzysztof Wende