Review: Mastering Unit Testing Using Mockito and Junit

Mastering Unit Testing Using Mockito and Junit
Mastering Unit Testing Using Mockito and Junit by Sujoy Acharya
My rating: 4 of 5 stars

Good reading. A complete picture on unit testing across applications layers and frameworks.

The author covers all topics related to unit testing Java based applications using jUnit at several application layers (data, business, web), starting from project setup (using Eclipse, ant, maven, gradle and even jenkins) and ending to tests creation best practices.

Even test beginners can benefit for this book because all explanations are clear and complete.

First four chapters explore unit testing concepts by means of jUnit and Mockito libraries.

Chapter five is dedicated to code coverage using both Eclipse plugins and command line tools (via ant, maven and gradle)

There is also a chapter (#6) dedicated to static code analysys with FindBus, PMD and SonrQube.

Remaining chapters are on web (#7) and data (#8) layers testing, on legacy code testing issues (#9) and, finally, on test best practices (#10).

View all my reviews

New Java releases: 8u20 and 7u67

 JDK 8

After the 8u11 release on mid of July, Oracle has released JDK 8u20 which includes several new and updated tools and a significant amount of bug fixes.

Java Mission Control has been updated to version 5.4. JMC is a tool to monitor and collect data from running applications and Java run-time environments.

AMC (Advanced Management Console) is a Java applications and versions usage collector and analysis tool. It requires a commercial license.

Another commercial product is the new JRE installer for Windows which fully support MSI technology.

Nice feature for 64bit users is in the Java Control Panel where we can select to automatically update  both 32bit and 64bit JRE.

Complete release notes can be found at

Bug fixes:

By the way, the famous Java 8 bugs which affected several third party tools like jRebel (see for example has been fixed. See for details.


New update also for Java version 7: 7u67. This version only includes the bug fix for the verifier regression (see above).

Complete release notes can be found at

Java 6 and previous versions are at end of life stage so they are no more updated.

Review: Mockito Cookbook

Mockito Cookbook
Mockito Cookbook by Marcin Grzejszczak
My rating: 5 of 5 stars

Very good. This is a well written and very interesting reading for everybody involved in Java testing. This book is not intended to be a primer on testing in general and on Mockito in particular. Beginners should look for an introduction on automated testing before start with Mockito Cookbook. The author covers everything related to the Mockito library and also on several additional libraries and tools connected to Mockito, like, for example, PowerMock. The book starts with an introductive chapter on Mockito installation and basic usage with both JUnit and TestNG. The core of the book is the chapters from 2 to 7 where the author explain, as recipes, several techniques to test easy and difficult to test code. Final book chapters are on legacy code testing, testing using frameworks like Spring and Mockito compared to other mocking libraries. In all examples, sometimes a bit repetitive due to the recipes approach, the author also explain general testing and object oriented programming approaches and methodologies. This is of course limited in space but there are plenty of references.

View all my reviews

My 5 favorite NetBeans IDE features (vs. Eclipse)

In my current project, Eclipse Kepler has been selected by the customer as the preferred IDE for all main tasks. Of course, I’m also using NetBeans 8.0 for some activities like, for example, legacy code test coding.

I have already used Eclipse some years ago and, at that time (2009), Eclipse Galileo were quite superior to NetBeans 6.x: faster, rich of code hints and warnings, full of useful plugins. So, I was not really happy when the project moved to NetBeans (because of its Swing support).

Now let me compare them again, Eclipse Kepler 4.3 vs NetBeans 8.0, on same project (a big ant based web project) and on the same PC (Win7 64bit 4G RAM).


My first big surprise. Five years ago, when I moved from Galileo to NetBeans I really missed Eclipse speed: coding, windowing, compilation, everything were significantly faster in Eclipse.

Now this speed advantage is gone. I don’t have benchmarks to demonstrate my feeling but now my coding is visibly faster with NetBeans 8.0 than with Eclipse Kepler. If you install Findbugs and PMD plugins, Eclipse becomes really slow.

Native testing support

Second surprise. Why, in 2014, an IDE for Java EE developers does not include full testing support ?

I cannot imagine any real EE developers which does not need any kind of test support from the IDE.

In NetBeans there is complete native jUnit and TestNG support (templates, test runs, jump to test option in the editor, both *Test and *IT files support) which I found very useful and efficient.

Eclipse native support is poor but of course it can be extended via plugin. I’m using MoreUnit plugin which is quite nice and add many missing features to Eclipse. See here for a small tutorial.

But even if you install a test support plugin like MoreUnit, still an important Eclipse limitation remains : the possibility to have a dir for test files separated from the main src dir, which is an essential feature to let us handling deployments. If you don’t believe me, have a look at this 2008 bug still open: and its related bugs.
Simply unbelievable.

Java Hints and FindBugs integration

While coding, it is important to have feedbacks from the IDE about the quality of the code and possible errors. NetBeans Java hints is a very reach set of the code checks which helps me to avoid stupid errors and keep high the quality of my work. In addition, NetBeans includes first class support for FindBugs which, again, helps me during coding sessions.

I’ve found NetBeans code quality checks support better than the one available in Eclipse Kepler + FindBugs plugin. More checks and better control on how, for example, disable a FindBugs warning on a line (because it is ok) using an annotation: just a click with NetBeans and by hand with Eclipse.

Coding support: templating, completion,interfaces…

I like NetBeans coding support: code templates (some letters + tab to get most used keywords and structures (see here for some examples), auto-completion, now also using sub words, I can easily jump from an interface to its implementation, the new indent guide lines.

Similar features are available in Eclipse using, for example, the Code Recommenders plugin but I’ve found it quite slow on my machine so I had to disinstall it.

User Interface

NetBeans user interfaces has improved a lot while keeping its strong point: it is easy to use. Windows and menus are easy to find and the full screen mode is very useful on small screens. I really like the color coding setup (Norway Today) which makes code reading a pleasure and the dark skins for working late in the evening.

On the contrary, Eclipse interfaces remains somehow confusing and, my personal opinio, perspectives and workspaces are simply a waste of time.

Tutorial: unit testing in Eclipse with moreUnit plugin

Eclipse Kepler, even in the Java EE Developers edition, does not include a good enough support for automatic tests development. Both NetBeans and IntelliJ have superior native support for testing. For example, if you rename one class, Eclipse does not rename its test class.

To overcome such limitations, we need to install a plugin.

In this tutorial, I will describe moreUnit (, a plugin which extends Eclipse testing support. I will not cover moreUnit installation because it is quite standard.

Let’s create a standard ant based Eclipse project. In the example the project name is “EclipseTest” and the main package is “it.gualtierotesta”.
In the package, I’ve created a new Java class named MyClass with the following content:

package it.gualtierotesta;

public class MyClass {

    private String msg;

    public String message() {
        return generateMsg();

    private String generateMsg() {
        return "Hello " + msg;

    public String getMsg() {
        return msg;

    public void setMsg(String msg) {
       this.msg = msg;


Very simple. We want now to create a unit test to check the message() method. While in the Java Editor window, you should press Ctrl+R (or, from the contextual menu, MoreUnit –> Jump To Test) to trigger test class creation.

where we can select our preferred unit library. In this tutorial we will use JUnit 4.

Press “Finish” to confirm. Note: Eclipse will eventually ask you to add JUnit library to the project build path.

In the created test class ( file), add the following contents:

package it.gualtierotesta;

import org.junit.Assert;
import org.junit.Test;

public class MyClassTest {

 public void showMessage() {
     // given
     MyClass sut = new MyClass();
     // when
     String res = sut.message();
     // then
    Assert.assertEquals("", res);


Note: sut is system under test.

In the test class window, press Ctrl+R to run the test. Test will fail:
20140522_03This is expected because the Assert.assertEquals check for the wrong result.
Change the Assert line as following:

Assert.assertEquals("Hello Gualtiero", res);

E re-run the test (Ctrl+R). Test will now succeed.
Note: in the test method body I added some comment lines to divide the body instructions in three sections. Given section is where the test texture is prepared, when is the execution of the method under test and the then section marks the results checks instructions (assertions). This is a good habit to make test strategy clearer. It comes from BDD, Behavior Driven Development (


A nice moreUnit feature is to mark the Java source file icon with a small green rectangle to show that the class has a test class.
20140522_05We have now our test running without failure but still our setup is not correct: test classes should be placed in a different source folder because they should not be packaged and released to the user.

Traditional approach is to have src dir for Java source files and test dir for test classes.

We create now a new folder named “test” under project root:
Then in the Java Build Path section of the project properties we add the new test folder as one of the project source folders:
Finally we configure moreUnit to use the “test” folder as the place to create and look for test classes:
Finally we can move the to the test folder (or create it again).

At the end, the project configuration is the following:
20140522_09As shown in this short tutorial, moreUnit plugin simplify a lot test classes handling in Eclipse.

Moreover, moreUnit offers special support for mocking libraries users. The test class creation wizard has additional pages to insert mocking specific instructions in the test class:
In this page, you can select the dependencies you want to mock and moreUnit will add specific mocking instructions. For more info about Mockito, have look here and here.

GDocx 3.0.0 released

GDocx, the fluent interface to the Docx4J library, version 3.0.0 has been released.

What’s new ?

GDocx now uses the latest (3.0.1) Docx4j version.

Other changes includes replacement of Fest Assert with the AssertJ library which is more flexible and powerful.

All files, including source and javadoc can be downloaded from the project home at the