dimanche 16 août 2020

Mocking database calls from external class

There is a library built by our company that is used as an external dependency by the other project we're currently creating tests for (Component Tests, to be more precise). Our goal is thus to let the code flow as deep as possible in all the classes of the pom.xml dependencies, but without requiring network calls.

We want to mock all external dependencies, and do not want to have to modify or fork those libraries from our company.

That library makes calls to an Oracle database using a JNDI connection.

The project runs on Tomcat, and the context.xml sets up the JNDI connection. We initially wanted to simply inject a H2 connection in there instead, but H2 isn't directly compatible with Oracle and the amount of work to achieve that is too big. There is a stored procedure that would need to be translated into a Java function (for the needs of H2 compatibility), among other things.

Is it possible to somehow intercept the calls made to the database, and return something we'd decide instead?

Solutions we're looking into, but haven't yet understood how to make it work:

  1. Something similar to how MockServer/WireMock work.
  2. Figuring out how to set up a proxy.
  3. Somehow injecting a different class than the one that actually calls the database.

We use Java 8, Spring 5 with XML configuration of beans, and Maven Cargo to start the Tomcat 8. The library uses Java 8, Spring 4, and the classes establishing connections to the database are not Beans.

Aucun commentaire:

Enregistrer un commentaire