Core Java


Sorry for not having written for some time but I was busy with writing the JBoss Drools Refcard for DZone and I am in the middle of writing a book about Mockito so I don’t have too much time left for blogging…

Anyway quite recently on my current project I had an interesting situation regarding unit testing with Mockito and JAXB structures. We have very deeply nested JAXB structures generated from schemas that are provided for us which means that we can’t change it in anyway.

Let’s take a look at the project structure:

The project structure is pretty simple – there is a Player.xsd schema file that thanks to using the jaxb2-maven-plugin produces the generated JAXB Java classes corresponding to the schema in the target/jaxb/ folder in the appropriate package that is defined in the pom.xml. Speaking of which let’s take a look at the pom.xml file.

The pom.xml :

<project xmlns="" xmlns:xsi=""





Apart from the previously defined project dependencies, as mentioned previously in the jaxb2-maven-plugin in the configuration node you can define the packageName value that defines to which package should the JAXB classes be generated basing on the schemaDirectory value where the plugin can find the proper schema files.

Speaking of which let’s check the Player.xsd schema file (simillar to the one that was present in the Spring JMS automatic message conversion article of mine):

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="">

    <xsd:element name="PlayerDetails">
                <xsd:element name="Name" type="xsd:string"/>
                <xsd:element name="Surname" type="xsd:string"/>
                <xsd:element name="Position" type="PositionType"/>
                <xsd:element name="Age" type="xsd:int"/>
                <xsd:element name="ClubDetails" type="ClubDetails"/>

    <xsd:complexType name="ClubDetails">
            <xsd:element name="TeamName" type="xsd:string"/>
            <xsd:element name="Country" type="CountryDetails"/>

    <xsd:complexType name="CountryDetails">
            <xsd:element name="CountryName" type="xsd:string"/>
            <xsd:element name="CountryCode" type="CountryCodeDetails"/>

    <xsd:complexType name="CountryCodeDetails">
            <xsd:element name="CountryName" type="xsd:string"/>
            <xsd:element name="CountryCode" type="CountryCodeType"/>

    <xsd:simpleType name="CountryCodeType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="PL"/>
            <xsd:enumeration value="GER"/>
            <xsd:enumeration value="FRA"/>
            <xsd:enumeration value="ENG"/>
            <xsd:enumeration value="ESP"/>

    <xsd:simpleType name="PositionType">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="GK"/>
            <xsd:enumeration value="DEF"/>
            <xsd:enumeration value="MID"/>
            <xsd:enumeration value="ATT"/>


As you can see I’m defining some complex types that even though might have no business sense but you can find such examples in the real life.

Let’s find out how the method that we would like to test looks like. Here we have the PlayerServiceImpl that implements the PlayerService interface:

package com.blogspot.toomuchcoding.service;

import com.blogspot.toomuchcoding.model.PlayerDetails;

 * User: mgrzejszczak
 * Date: 08.06.13
 * Time: 19:02
public class PlayerServiceImpl implements PlayerService {
    public boolean isPlayerOfGivenCountry(PlayerDetails playerDetails, String country) {
        String countryValue = playerDetails.getClubDetails().getCountry().getCountryCode().getCountryCode().value();
        return countryValue.equalsIgnoreCase(country);

We are getting the nested elements from the JAXB generated classes. Although it violates the Law of Demeter it is quite common to call methods of structures because JAXB generated classes are in fact structures so in fact I fully agree with Martin Fowler that it should be called the Suggestion of Demeter. Anyway let’s see how you could test the method:

    public void shouldReturnTrueIfCountryCodeIsTheSame() throws Exception {
        PlayerDetails playerDetails = new PlayerDetails();
        ClubDetails clubDetails = new ClubDetails();
        CountryDetails countryDetails = new CountryDetails();
        CountryCodeDetails countryCodeDetails = new CountryCodeDetails();

        boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetails, COUNTRY_CODE_ENG);

        assertThat(playerOfGivenCountry, is(true));

The function checks if, once you have the same Country Code, you get a true boolean from the method. The only problem is the amount of sets and instantiations that take place when you want to create the input message. In our projects we have twice as many nested elements so you can only imagine the number of code that we would have to produce to create the input object…

So what can be done to improve this code? Mockito comes to the rescue to together with the RETURN_DEEP_STUBS default answer to the Mockito.mock(…) method:

    public void shouldReturnTrueIfCountryCodeIsTheSameUsingMockitoReturnDeepStubs() throws Exception {
        PlayerDetails playerDetailsMock = mock(PlayerDetails.class, RETURNS_DEEP_STUBS);
        CountryCodeType countryCodeType = CountryCodeType.ENG;

        boolean playerOfGivenCountry = objectUnderTest.isPlayerOfGivenCountry(playerDetailsMock, COUNTRY_CODE_ENG);

        assertThat(playerOfGivenCountry, is(true));

So what happened here is that you use the Mockito.mock(…) method and provide the RETURNS_DEEP_STUBS answer that will create mocks automatically for you. Mind you that Enums can’t be mocked that’s why you can’t write in the Mockito.when(…) function playerDetailsMock.getClubDetails().getCountry().getCountryCode().getCountryCode().getValue().

Summing it up you can compare the readability of both tests and see how clearer it is to work with JAXB structures by using Mockito RETURNS_DEEP_STUBS default answer.

Naturally sources for this example are available at BitBucket and GitHub.

Reference: Mockito – RETURNS_DEEP_STUBS for JAXB from our JCG partner Marcin Grzejszczak at the Blog for coding addicts blog.

Marcin Grzejszczak

Senior Java Developer with team building and managing skills. Enthusiast of JVM languages (Groovy, Scala) and clean coding
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button