Compiling your project successfully
For Diffblue Cover to write tests successfully, it needs your code to both compile and run. For the best results, your project needs to be completely built from the root. If this doesn’t work, you may have an error in your built dependencies.
What could be the problem?
Being able to execute your unit tests means that the following must be available:
- All class files that are produced by compilation of your project
- All the dependencies (i.e. other modules in your project and jar files). Note that jar files contain class files and may, in turn, depend on other jar files.
These jar files are typically downloaded by your build system (e.g. Maven or Gradle).
(You also need the Java Class Library provided with your JDK, but Diffblue Cover will work out your JDK version and find the appropriate classes.)
Sometimes you might succeed in compiling your code, but when you run either your application or your tests you receive an error that a class was not found. This happens when dependencies are missing that were not necessary for compilation, but they are still required for execution.
“Class not found” errors indicate that the project wasn’t compiled correctly and/or that something is wrong or missing in your build configuration (e.g. pom.xml) or your Java environment. In these situations, Diffblue Cover is unable to run the unit tests it writes until you can fix these problems.
Fixing build or compilation problems
First, ensure that you have compiled multi-module projects from the root, using a command such as:
$ mvn clean install -DskipTests
Please note that even though you have to run Diffblue
dcover from within the module you want to create tests for, you have to run the compilation command from the root of your project, not from within a module.
The example below uses the multi-module Maven project JDMN. Download the project and switch to the release 3.10.0:
git clone https://github.com/goldmansachs/jdmn cd jdmn git checkout 3.10.0
If we simply run
mvn compile the project does not compile correctly, and dcover is unable to write tests. Use the command
mvn clean install -DskipTests instead to compile the project and write tests successfully.
Another common mistake is to use the correct command but inside a module, for example:
$ cd dmn-tck-it $ mvn clean install -DskipTests
In this example, the compilation ‘succeeds’, but hides other problems. All the required dependencies were downloaded by Maven from the central repository. One of these dependencies was
dmn-core, a module provided by the same project. But the version of that module that Maven downloaded may not match the code we have in the local module, or be able to compile it, e.g.
$ ls ../dmn-core/target ls: ../dmn-core/target: No such file or directory
This could be a problem if, for example, we make changes to
dmn-core and expect the code in
dmn-tck-it to exercise those changes.
To fix this, return to the project root and run
mvn clean install -DskipTests from there:
$ cd .. $ mvn clean install -DskipTests
You can now change directory into the module that you would like to create tests for, and run
dcover create. If this still does not work, then please look at your build configuration.
Finding errors in your build configuration
Typical problems arise from system scope dependencies and exclusions:
- System scope dependencies are sometimes used to bake commonly used dependencies into a prepared environment (e.g. server or CI image). So it could happen that these dependencies are not installed in your local environment. The solution is to ensure that your local environment conforms to the runtime environment on your servers or CI system.
- Exclusions specify that certain transitive dependencies should be excluded from your classpaths. It can happen that such an exclusion is incorrect, excluding classes that are actually needed. The solution in this case is to find the faulty exclusion and fix your build configuration.