Unit Testing

Valid from Datafari 3.x

Datafari 3.x comes with some Unit Tests (at last !)

Four frameworks for Unit Testing are embedded in this version of Datafari :

  • JUnit : the standard JAVA Unit Testing framework

  • Mockito : famous framework that allow to mock JAVA objects

  • XMLUnit : very useful framework for XML comparison

  • testcontainers: allows to setup a Cassandra container for integration tests

We think that these four frameworks cover more than 90% of the needs when creating a Unit Tests, so now you have no good excuses to create Unit Tests when it is possible for your development.

Now, let's try to explain our usages and good practices of the Unit Tests in Datafari:

- The first thing to know is that all the existing and future Unit Tests have to be located in Datafari_Home/src/test. As you will notice, there are two sections in this directory:

  • main/java : That contains all the JUnit JAVA classes

  • resources : That contains the resource files needed by the Unit Tests. Each Test which is using one or several resource files, must have his own repertory in this section. It is then easier to access to the resource files corresponding to a specific Test

To be able to run the tests from the eclipse environment, you will need to add a Variable to the classpath of Datafari :
Right click on the project → Build Path → Configure Build Path

Now go to the libraries tab and click on "Add Variable", then "Configure Variables" and click on "New". Name it "DATAFARI_CLASSPATH" and for the Path browse to the Datafari_Home/dist/WEB-INF/classes.Then click "OK" till you return to the Libraries tab and click "Apply" then "OK"

- The second point is that the ant script "datafari-dev.xml" which is used to compile and deploy the web part of Datafari (inluding the servlets) to the tomcat server, is also configured to automatically run all the JUnit tests found in the Datafari_Home/src/test/main/java directory. So when you are done with a new development or a modification of Datafari, check the output of this ant script to see if all the tests have passed with success.

- The third and last thing to understand is the kind of Unit Tests we are expecting in Datafari. The usage of Unit Testing is limited in Datafari, because the product has many interactions with tier applications (ELK, Solr, ManifoldCF, Cassandra) and that it makes no sense to try to mock those components as they represent the core of Datafari. So, based on this observation, we have focused our Unit Tests on another important part : the manipulation of the configuration files. It applies for now to the config files of Datafari, Solr and ELK.  Our tests generate calls to the servlets in charge of the modifications/access to the config files and compare their behavior and their outputs to the expected ones. In that way we insure well formed configuration files and presence of every expected parameters and values. 

So now that you know what is our goal with the Unit Tests and that Datafari provides you the tools to make your owns, we hope and encourage you to enrich Datafari with a lot of Unit Tests.

About testcontainers

Test containers is used only in datafari-webapp, and in particular in the TestDataServices.

As testcontainers uses docker, you need to have docker installed and a docker environment available to run those tests.

As this is not the case for everyone, there is a way to build Datafari without running those tests. To do so, launch the build as follows:

mvn install -DfailIfNoTests=false -Dtest='!TestDataServices'