Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

How to test your Java Application using PMD, and Jenkins

1 min read

image formats

In the previous articles, Previous whilst working on various projects at IDR Solutions I went through the process of how to build your own Java Application with Apache Ant and also how to set up Jenkins. In this tutorial i will show you how to perform automated static analysis test on your source code.
I will suggest you see the previous article before proceeding if you haven’t. This will make it easier to follow.

What is PMD

It is a static rule-set based Java source code analyser that identifies potential problems in your source code. There are other static analysers like FindBugs. You can get PMD from here

From our previous AntProject, open the jbuild.xml file. you can get the code here if you do not have it. Now in the jbuild file, copy and paste this code before the closing project tag. Also change the pmd.dir property value to where u installed you pmd

 <!-- ================== PMD : static analysis checks on code =================== -->

Now if running on MAC/LINUX, create a bash script called testSA.bash, copy the script below, change the ANT_HOME to where you installed ant paste.

export ANT_HOME=/PDFdata/library/apache_ant/
export PATH=$ANT_HOME/bin:$PATH
echo "Build PMD"
ant  -buildfile jbuild.xml pmd
if [ $? -eq 1 ]; then
echo "Failed on testSA"
exit 1

On Windows, make sure your ANT_HONE Environment variable is set, create a batch file the copy and paste the code below

echo "Build PMD"
ant  -buildfile jbuild.xml pmd
echo "Failed on testSA"
exit 1

Now we need to create the a file that will have all the pmd rules we want o check. Create a file called pmdRules.xml in the AntProject and paste this code

<!--?xml version="1.0" encoding="UTF-8"?-->
This ruleset checks my code for bad stuff
      <!-- Checking for unused imports -->
        <!-- Check for Empty Catch blocks -->
       <!-- Check for Empty try blocks -->

In the xml file above, we are only checking for unused import and empty try and catch blocks.You can find more rules here. Your project should now be looking like this.

Screen Shot 2015-07-15 at 16.26.19
Now open your command line, navigate to your ant project and run the bat/bash script.You should see something like this

Screen Shot 2015-07-15 at 16.33.32
Time to break the build. Now that it successfully builds, open your java class, add an import statement and empty try and catch block.

Screen Shot 2015-07-15 at 16.44.14
Open command line and run testSA again, this time it should fail.

Screen Shot 2015-07-15 at 16.52.55
Head over to NetBeans, in your project you should see a new file called pmd_report.html. Right click on it and select view, this should tell you why the test is failing.

Screen Shot 2015-07-15 at 16.58.55
Screen Shot 2015-07-15 at 16.57.20
Now Let’s test with Jenkin.
Refer to previous article to set up Jenkins task.
To do that I am going to push my go to GitHub and manually run it for the first time.

Screen Shot 2015-07-15 at 17.29.58

As you can see it pulled from my GitHub repo and was started by me.

Now i will push a code that will break the test.

Screen Shot 2015-07-15 at 17.50.42

As you can see this one was performed by SCM change. so each time you push to GitHub the test automatically runs and sends you an email if it failed.

Screen Shot 2015-07-15 at 17.58.43
Hopefully you have found this quick guide useful.

Our software helps Java Developers to

View PDF files in a web application →

Parse PDF files as HTML →

Display PDF Forms in a web browser →

View PDF Documents in Java Applications →

Rasterize PDF Documents to image →

Read/Write images (including HEIC, JPG and WEBP) →

Convert Image files to PDF Documents →

Ernest Duodu Ernest is a Java developer. Outside programming, he also enjoys a wide variety of hobbies which includes sky-diving, photography, exercising and listening to music.

Leave a Reply

Your email address will not be published. Required fields are marked *

IDRsolutions Ltd 2022. All rights reserved.