I have a web application that defines Hibernate Integrator as part of the Java ServiceLoader specification, for example:
Src / main / resources / META-INF / services / org.hibernate.integrator.spi.Integrator
This is done according to the Hibernate guide here .
My problem is that when I try to run unit tests, the main Integrator descriptor is still parsed and created. This means that because I mock most of the application in unit tests, when the integrator tries to run it, it encounters errors that make my tests fail.
I defined the same file in test resources:
Src / test / resources / META-INF / services / org.hibernate.integrator.spi.Integrator
but instead, I find that the analytic and core files of the integrator are being analyzed.
I expected the test resource to overwrite the main resource, thereby providing the main obscolete resource, but this is not what happens. Since both files are in the class path (I run tests through Maven using a valid plugin that puts both test-classes and classes in the class path). The same thing happens with persistence.xml .
In my modular test environment, I do not want any integrators to be created, because I want to manage the construction of these beans as quickly as possible. Let me test the units of execution, I donβt want additional beans, such as integrators lying around, to affect the operation of the tests. I think this is an absolutely legal requirement during unit testing. However, while the main resources are still being processed by ServiceLoader , this is not possible.
The solution I came up with is based on the persistence.xml solution posted here:
How to configure JPA for testing in Maven
My question is, is there a better way to exclude core processing resources during unit testing than force renaming, especially in the context of ServiceLoader files?
Try and summarize it a little better:
What happens if you have two files, both of which are named after the same service interface in the classpath? It seems to me that all services in both files are created. There seems to be no rewriting.