Thursday, July 15, 2010

Automate Your Testing using FindBugs with Eclipse Step 59

Another tool that you might want to get familiar with is FindBugs. FindBugs is a static analysis tool that looks for bugs in a Java program based on concepts of bug patterns.

You can install this tool to run as an ant script or with the GUI using the eclipse plugin.

To use FindBugs with eclipse and Ant, follow these instructions.

Download findbugs-1.3.7.zip from this website: http://findbugs.sourceforge.net/downloads.html

You should unzip it and set an environmental variable to it called Find_Bugs.
To set an environmental variable, you will select your "my Computer" icon with your right mouse button. Then you should select properties->Advanced Tab->Environmental Variable->New.

Set the Name to be: Find_Bugs and set the value to be the path of Find Bugs: C:\findbugs-1.3.7\findbugs-1.3.7

To install the FindBugs plugin on Eclipse Ganymede, you should open up eclipse, select Help->Software Updates->Available Software->Add Site

You want to add these two sites:
http://findbugs.cs.umd.edu/eclipse/
http://www.jutils.com/eclipse-update

Select the Findbugs from available software and select install.

Click Next, and accept the license terms and agreements.

Click Finish and install all.

Installing this might give you an error when restarting eclipse. If that happens, go into your eclipse workspace and delete the .metadata folder and then restart eclipse. After deleting the .metadata folder, you will need to import your project again. To import your project, select the import from the file menu and follow the instructions.

If you need to input a login and password to connect, just input guest as the login.

In your build.xml file, you should add this script:

<taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask">
<classpath><pathelement location="${env.FIND_BUGS}/lib/findbugs-ant.jar"/></classpath>
</taskdef>

<target name="findbugs">
<echo>FindBugs</echo>
<findbugs home="${env.FIND_BUGS}" output="xml" outputFile="fb.xml">
<sourcepath path="${JAVA_SRC_DIR}"/>
<class location="${JAVA_BUILD_DIR}/UserBook.jar"/>
<auxclasspath refid="class.path" />
</findbugs>
</target>

You should also set the dependency on your test target that says:
depends="findbugs". This will run your findbugs, and then your test before your code is built. UserBook.jar is the jar file of your project.

The taskdef will define the class that is needed to define the findbugs target.

Before your war file is built, your static analysis tool and your test cases would run first.

If you get the error below, the cause is because you do not have the findbugs task defined correctly within your eclipse.

Could not create task or type of type: findbugs.

Ant could not find the task or a class this task relies upon.

You need to define your taskdef name="findbugs" as I state above in your ant script.

To use the plugin, you will select on your project and select findbugs.

To see your results, you can select Window from your top menu, and select Show View, then Other, then FindBugs, and then bug explorer.


The plugin will show you the findbugs results.

Go To Step 60

0 comments: