<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Giordano Scalzo&#039;s Personal Blog &#187; trick</title>
	<atom:link href="http://giordano.scalzo.biz/tag/trick/feed/" rel="self" type="application/rss+xml" />
	<link>http://giordano.scalzo.biz</link>
	<description>Just another useless weblog</description>
	<lastBuildDate>Mon, 09 May 2011 09:50:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>No more excuses 2: junit testing System time</title>
		<link>http://giordano.scalzo.biz/2009/11/07/no-more-excuses-2-junit-testing-system-time/</link>
		<comments>http://giordano.scalzo.biz/2009/11/07/no-more-excuses-2-junit-testing-system-time/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 13:59:37 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[trick]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=197</guid>
		<description><![CDATA[Another common exemple of static dependency, it&#8217;s the Time System dependency. In greenfield project it could be possible inject time into the objects they&#8217;ll use it, but in legacy code it&#8217;s more difficult, so oftet we gave up and don&#8217;t unit test some behaviour dependent from time. In a recent project, I had the problem [...]]]></description>
			<content:encoded><![CDATA[<p>Another common exemple of static dependency, it&#8217;s the Time System dependency.<br />
In greenfield project it could be possible inject time into the objects they&#8217;ll use it, but in legacy code it&#8217;s more difficult, so oftet we gave up and don&#8217;t unit test some behaviour dependent from time.</p>
<p>In a recent project, I had the problem to check the correct format of a filename that it had to contain the creation timestamp; I easily overcame it with a nice test pattern made by <a href="http://c2.com/cgi/wiki?PaoloPerrotta">Paolo &#8220;Nusco&#8221; Perrotta</a>, well known <a href="http://pragprog.com/titles/ppmetr/metaprogramming-ruby">Ruby Metaprogramming guru</a>, in a version wrote by <a href="http://bbossola.wordpress.com/">Bruno Bossola</a> in a nice <a href="http://www.vqwiki.org/">Open Source project</a>.</p>
<p>In a nutshell, the trick is to <em>adapt </em>System Time with another object.</p>
<p>Let see a simple example.<br />
This is the class to test:</p>
<pre class="brush: java">
package biz.scalzo.prod;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeFormatter {
	public String now() {
		SimpleDateFormat sdf = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm");
		return sdf.format(new Date(System.currentTimeMillis()));
	}
}
</pre>
<p>I know it mixes two responsibilities, it has a silly static dependecy and so on, but forgive my oversemplification used to show the pattern.</p>
<pre class="brush: java">
package biz.scalzo.test;

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

public class ATimeFormatter {
	@Test
	public void shouldFormatNowInFancyWay() {
		Assert.assertEquals("2009/10/13 15:29", new TimeFormatter().now());
	}

}
</pre>
<p>The behaviour to check is how well the Formatter formats current time in string; we have to find a way to set a well know time point so we can check te result of format.</p>
<p>To implement the <a href="http://c2.com/cgi/wiki?VirtualClock">VirtualClock</a> pattern, we declare a simple <em>Clock </em>interface:</p>
<pre class="brush: java">
package biz.scalzo.prod;

public interface Clock {
	public long now();
}
</pre>
<p>and a repository where we can insert our fake clock</p>
<pre class="brush: java">
package biz.scalzo.prod;

import java.util.Date;

public class SystemTime {
	private static Clock clock;

	private static final Clock defaultClock = new Clock() {
		public long now() {
			return System.currentTimeMillis();
		}
	};

	public static long asMillis() {
		return getClock().now();
	}

	public static Date asDate() {
		return new Date(getClock().now());
	}

	public static void reset() {
		clock = null;
	}

	public static void setClock(Clock aClock) {
		clock = aClock;
	}

	static Clock getClock() {
		return (clock != null ? clock : defaultClock);
	}
}
</pre>
<p>Now we can change our class using <em>SystemTime </em>instead of <em>System.currentTimeMillis(</em>):</p>
<pre class="brush: java">
package biz.scalzo.prod;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeFormatter {
	public String now() {
		SimpleDateFormat sdf = new SimpleDateFormat(
				"yyyy/MM/dd HH:mm");
		return sdf.format(SystemTime.asDate());
	}
}
</pre>
<p>In test harness we can inject a fake clock</p>
<pre class="brush: java">
package biz.scalzo.test;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import biz.scalzo.prod.Clock;
import biz.scalzo.prod.SystemTime;
import biz.scalzo.prod.TimeFormatter;

public class ATimeFormatter {
	private static final long FAKED_TIME = 1255440571354L;

	private void fakeClock() {
		SystemTime.setClock(new Clock() {
			public long now() {
				return FAKED_TIME;
			}
		});
	}

	@Before
	public void setUp(){
		fakeClock();
	}

	@Test
	public void shouldFormatNowInFancyWay() {
		Assert.assertEquals("2009/10/13 15:29", new TimeFormatter().now());
	}

}
</pre>
<p>and we have green bar! Neat, isn&#8217;t it?</p>
<p>I know it&#8217;s a lot of code, but in legacy code it&#8217;s better than leave a feature without a test.</p>

<!-- start wp-tags-to-technorati 1.02 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/java' rel='tag' target='_self'>java</a>, <a class='technorati-link' href='http://technorati.com/tag/test' rel='tag' target='_self'>test</a>, <a class='technorati-link' href='http://technorati.com/tag/trick' rel='tag' target='_self'>trick</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/11/07/no-more-excuses-2-junit-testing-system-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

