SessionFactory bean definition ignores test-hibernate.cfg.xml in event-api-tests jar

TLDR:

The ContextSensitiveTests fail on few machines with TableNotFoundException when including event-api tests jar. The sessionFactory bean definition in TestingApplicationContext doesn’t include the test-hibernate.cfg.xml configLocation. The tests fail if spring uses this definition of sessionFactory bean because it cannot find the tables referred in test-hibernate.cfg.xml.

Longer Version

Context:

We are using event-module in our project and we started writing ContextSensitiveTests for the last few days back. Since the time we have added the event-api-tests.jar we have seeing flaky test failure with errors as NoSuchTableException for our local tables. It doesn’t fail on all machines but on some machines it fails. Our tables has been referred in test-hibernate.cfg.xml in our local omod.

Troubleshooting:

We started debugging this problem and figured out sessionFactory#configLocations is populated with just one location(classpath:hibernate.cfg.xml) in the scenarios the tests are failing. The scenarios where it passes the same is populated with two locations (classpath:hibernate.cfg.xml and classpath:test-hibernate.cfg.xml).

We started checking if there is a sessionFactory bean getting defined with just one location in any of the TestingApplicationContext.xml including our libraries. We found out the event-api does that and we have started seeing the failure after including the event-api-tests.jar.

Possible Fix:

We suspected this might be happening because of which definition of sessionFactory spring is useing in a test run. To fix this problem we added a blank test-hibernate.cfg.xml in our event-api fork and mentioned that in the TestingApplicationContext.xml of event-api, so if spring uses this definition of sessionFactory it doesn’t ignore the test-hibernate.cfg.xml present in our local module files.

Questions:

  • While we have tested with the fix above and it is working, is there another way of fixing this problem?
  • Has anyone else used event-api-tests jar and faced a similar problem?

The changes mentioned are pushed here, we can raise a PR for this, if it makes sense.

@dkayiwa @mksd @angshuonline @rrameshbtech @sukreet @wyclif

This:

<property name="configLocations">
  <list>
    <value>classpath:hibernate.cfg.xml</value>
    <value>classpath:test-hibernate.cfg.xml</value>
  </list>
</property>

is how every TestingApplicationContext.xml should be defined, so I would suggest to raise the PR.

Great troubleshooting @mddubey, @premnisha, @sukreet

Thanks @mksd.

We will raise a PR with the same fix quickly. Once merged and snapshot is published we will change our project to use latest snapshot version.

Raised a PR for this.