I love PowerMock! For those who don’t know it, quoting their site, “PowerMock is a Java framework that allows you to unit test code normally regarded as untestable”.

While not one of its unique selling points, because EasyMock allows you to do basically the same, here’s an example of partially mocking a class in PowerMock.

Given class CustomerService:

class CustomerService {

	public void add(Customer customer) {
		if (someCondition) {
			subscribeToNewsletter(customer);
		}
	}

	void subscribeToNewsletter(Customer customer) {
		// ...subscribing stuff
	}
}

So you want to test the add() method for actually invoking subscribeToNewsletter() and do NOT want to execute the logic from subscribeToNewsletter() in this test – e.g. since you’re already unit testing subscribeToNewsletter() somewhere else.

Then you create a PARTIAL mock of CustomerService, giving a list of methods you want to mock.

CustomerService customerService = PowerMock.createPartialMock(CustomerService.class, "subscribeToNewsletter");
customerService.subscribeToNewsletter(anyObject(Customer.class));

replayAll();

customerService.add(createMock(Customer.class));

So add() within the CustomerService mock is the REAL thing you want to test and for the method subscribeToNewsletter() you now can write an expectation as usual.

Disclaimer:

  • Mocking partially like this only works with PUBLIC or DEFAULT methods. So for this one, I actually had to change subscribeToNewsletter() from PRIVATE to DEFAULT visibility to make it testable – which possible might not be desirable in all cases.
  • Using a string “subscribeToNewsletter” which matches an actual method name is not very refactoring safe. Preferable you should at least a constant to discriminate from being any ordinary string. Anyway, your unit test will break sooner or later anyway if this method changes name – and you’ll know :-)

For further reading, where PowerMock really excels is mocking of static methods and private methods.

The stats helper monkeys at WordPress.com mulled over how this blog did in 2010, and here’s a high level summary of its overall blog health:

Healthy blog!

The Blog-Health-o-Meterâ„¢ reads Fresher than ever.

Crunchy numbers

Featured image

A Boeing 747-400 passenger jet can hold 416 passengers. This blog was viewed about 6,300 times in 2010. That’s about 15 full 747s.

 

In 2010, there were 7 new posts, growing the total archive of this blog to 19 posts. There were 18 pictures uploaded, taking up a total of 4mb. That’s about 2 pictures per month.

The busiest day of the year was July 1st with 139 views. The most popular post that day was GORM and Hibernate’s session factory.

Where did they come from?

The top referring sites in 2010 were grailstutorials.com, dzone.com, grails.org, observatoriodegrails.com, and pedrocorreia.net.

Some visitors came searching, mostly for handen en voeten geven, eclipse repository registry initialization, david hasselhoff, grails form validation, and initializing java tooling.

Attractions in 2010

These are the posts and pages that got the most views in 2010.

1

GORM and Hibernate’s session factory July 2010
4 comments

2

Grails remote jQuery form validation March 2010
2 comments

3

Eclipse “Initializing Java Tooling” hangs June 2010

4

Separating and securing Grails controllers June 2010
1 comment

5

The Do-It-Yourself Posable Paper of David Hasselhoff May 2009

Follow

Get every new post delivered to your Inbox.