After a happy couple of weeks, the method I had written about here started to fall apart, at least in acceptance tests.

The issue was that in some acceptance tests, the Geb framework was using java.util.Calendar internally, and now as the Calendar was stubbed globally, it wasn’t working correctly anymore.

Thankfully, this issue was easily solved with a layer of redirection: I modified my code to use a “utils” class to access Calendar, and then when need be I stubbed the utils class instead.

class DateUtils {
	boolean isTodayMonday() {
		def cal = Calendar.getInstance()
		return cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY
	}
}
// Stubbing DateUtils to always return true when isTodayMonday is called
DateUtils.metaClass.static.isTodayMonday = { Calendar.MONDAY }