DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Incompatibility between TDD, Mocking and Static Methods

Incompatibility between TDD, Mocking and Static Methods

Marco Tedone user avatar by
Marco Tedone
·
Apr. 05, 11 · Java Zone · Interview
Like (0)
Save
Tweet
6.57K Views

Join the DZone community and get the full member experience.

Join For Free

Today I installed Jenkins CI and started building PODAM. I write all my code using TDD. The public API in PODAM offers a single method for now:

POJO myPojo = PodamFactory.createDummyPojo(POJO.class);

I decided to provide developers with a very simple-to-use API . For a complete list of PODAM requirements, please refer to my previous post.

So far I just wrote a single unit test which led to this piece of functionality:

     /**
* Generic method which returns an instance of the given class filled with
* dummy values
*
* @param <T>
* The type for which a filled instance is required
* @param dto
* The name of the class for which an instance filled with values
* is required
* @return An instance of <T> filled with dummy values
*
* @throws PodamMockeryException
* if a problem occurred while creating a POJO instance or while
* setting its state
*/
public static <T> T mockDto(Class<T> dto) {

try {
return dto.newInstance();
} catch (InstantiationException e) {
throw new PodamMockeryException(
"An exception occurred while instantiating " + dto, e);
} catch (IllegalAccessException e) {
throw new PodamMockeryException(
"An exception occurred while instantiating " + dto, e);
}

}

 

The unit test is as follows:

@Test
public void testSimpleDtoGraphGetsSet() {

SimpleGraphTestPojo dto = PodamMocker.mockDto(SimpleGraphTestPojo.class);
Assert.assertNotNull("The object cannot be null!", dto);

}

 

This is obviously only the beginning but following TDD practices it was my first passing test.

I then decided to use the Jenkins CI Emma plugin to check code coverage and was really surprised when I got 50% at class level. Upon investigation the Emma plugin showed me that I didn't test the branches for InstantiationException and IllegalAccessException. Static methods cannot be mocked since these are resolved at compile time.It turns out that either one has to write an interfaced service (maybe exposed as a Singleton) or that one has to give up testing every single branch of a static method when such branch throws Exceptions difficult to reproduce.

Nowhere is written that service classes (e.g. non instantiable classes with static methods) are a bad thing; on the contrary, Effective Java 2nd Edition, in Item 4, suggests that such utility classes have their uses. As a developer, if I had to decide between:

POJO myPojo = PodamFactory.createDummyPojo(POJO.class);

and

PodamFactory factory = PodamFactoryImpl.getInstance();//Singleton

POJO myPojo = factory.createDummyPojo(POJO.class);

I'd prefer the former; the latter looks a lot of boilerplate code. 

So it turns out that if one wants to use utility classes which expose static methods, and such methods throw internallly exceptions difficult to reproduce, one has to give up full coverage. Between having 100% coverage and ugly syntax on one side and 50% coverage with easy-to-use syntax on othe other, I chose the easy syntax.

 

From http://tedone.typepad.com/blog/2011/04/incompatibility-between-tdd-mocking-and-static-methods.html

unit test

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • MongoDB vs. DynamoDB Head-to-Head: Which Should You Choose?
  • Data Mesh — Graduating Your Data to Next Level
  • 3 Best Tools to Implement Kubernetes Observability
  • Top ALM Tools and Solutions Providers

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo