Test data not cleared between tests

Application Name: Platform Version Number: 2.0.5

Question: I have a suite of test cases in JUnit that call BaseContextSensitiveTest.executeDataSet in an @Before annotated method in some tests, and others tests call executeDataSet in the test method itself. In later tests after loading data for several tests beforehand, I get a SQL error saying I’m breaking a foreign key constraint. However, the foreign key error references a table that doesn’t have data loaded in the current test class, but that was loaded in previous test classes.

When I step through the code, I see that BaseContextSensitiveTest.closeSessionAfterEachClass gets called at the correct time. After stepping into deleteAllData() and getConnection(), an error occurs. Spring returns the correct bean for the session factory, but after calling getCurrentSession(), a null value is returned and an error is thrown when trying to call doReturningWork. I’ve checked and tried to call getConnection() from within an method with an @After attribute and everything works. When I try to delete all data in the @Before method or at other times, I seemingly get an infinite loop.

Does anyone have any insight into why this is the case and the data isn’t being cleared? Is there a problem with calling executeDataSet at the wrong time? @dkayiwa I see you first made the change to delete data in the @AfterClass method. Any thoughts?

For additional information, I have a module, OpenHMIS Cashier, running on platform 1.9.9 that I’m trying to update to 2.0.5.

Hi Kevin,

Though this is not the answer you are looking for, we generally now recommend using mocks for new tests. Did you evaluate that? https://wiki.openmrs.org/display/docs/Mock+Doc

Daniel,

I’m not exactly sure why these tests aren’t mocking services. Unfortunately, I also won’t be able to check on the reason for that for a while, maybe even a few months. As soon as I know the answer to that, if I can still meet the focus of the tests by updating to using mocks, and don’t break any backwards compatibility by switching, I can get to work on that.

Until then, I’ll keep trying to figure this out. If you stumble across anything, please let me know. I’ll also put an update if I find a working solution.

How does your test class look like?

This class has a @Before method in a parent class that creates a service by calling the OMRS Context.getService() method with the class name. Then, the child test class has a dataset loaded in it’s @Before annotated method.

After this, the tests go through the entire range of CRUD functionality via the service that was created in the parent. Each test makes a modification, calls Context.flushSession(), and fetches the entity before asserting the saves happened correctly and the DB holds what is expected.

So, it seems these tests are there to ensure OMRS saves the module’s data to the DB as expected. If so, mocking wouldn’t really help here. But, that’s what I need to find out.

Is the module publicly available for us to try reproduce and investigate?

It is, but it also has several dependencies. While these are all also publicly available, not all of the dependencies work with platform 2.x at the moment. I have updates and pull requests for each of them at the moment to make them work with the 2.x platform, but no merges.

So, you could pull my pull requests for OpenHMIS Commons, OpenHMIS Backbone Forms, Jasper Reports (including a change you currently have a PR for to fix a Cashier issue), and OpenHMIS Inventory. Not sure you want to do that, but you’re welcome to it if you’d like.

I should hopefully have these PRs merged into their respective DEV branches by next week.