<?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; tdd</title>
	<atom:link href="http://giordano.scalzo.biz/tag/tdd/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>iPhone Unit Test tips: Read a file in your test</title>
		<link>http://giordano.scalzo.biz/2011/04/18/iphone-unit-test-tips-read-a-file-in-your-test/</link>
		<comments>http://giordano.scalzo.biz/2011/04/18/iphone-unit-test-tips-read-a-file-in-your-test/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 10:44:27 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[bdd]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=334</guid>
		<description><![CDATA[Very often I need to unit test something read from a file, but it seems very difficult to accomplish this simple task in iOS, so usually I give up. But today I felt was a good day to find a solution In my code I want to read a JSon data from a file, so [...]]]></description>
			<content:encoded><![CDATA[<p>Very often I need to unit test something read from a file, but it seems very difficult to accomplish this simple task in iOS, so usually I give up.<br />
But today I felt was a good day to find a solution <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
In my code I want to read a JSon data from a file, so I wrote a simple test:</p>
<pre class="brush: cpp">
describe(@"Conference", ^{
    it(@"loads given a filename", ^{
        Conference *conference = [[Conference alloc]initWithFile:@"talks_with_three_tracks"];
        [[theValue([conference hasData]) should] beTrue];
    });
});
</pre>
<p>with a simple init method:</p>
<pre class="brush: cpp">
-(id)initWithFile:(NSString *)filename{
    if ((self = [super init])) {
        NSString* path = [[NSBundle mainBundle] pathForResource:filename ofType:@"json"];
        NSError *error;
        NSString* talksString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&#038;error];
        talks = [talksString JSONValue];
    }
    return self;
}

-(BOOL)hasData{
    return talks!=nil;
}
</pre>
<p>Unfortunately it doesn&#8217;t work <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> , because path is nil;<br />
But here it isthe solution:<br />
instead of using <code>[NSBundle mainBundle]</code>, we should use the bundle associated with our class:</p>
<pre class="brush: cpp">
        NSString *path = [[NSBundle bundleForClass:[Conference class]] pathForResource:filename ofType:@"json"];
</pre>
<p>Simple and neat: thank you <a href="http://stackoverflow.com/questions/3067015/ocunit-nsbundle">StackOverflow</a> <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>

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

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

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2011/04/18/iphone-unit-test-tips-read-a-file-in-your-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No more excuses: junit testing log messages</title>
		<link>http://giordano.scalzo.biz/2009/10/21/no-more-excuses-junit-testing-log-messages/</link>
		<comments>http://giordano.scalzo.biz/2009/10/21/no-more-excuses-junit-testing-log-messages/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 17:46:52 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=192</guid>
		<description><![CDATA[Althought I&#8217;m a straight adept of Tdd, sometime I can&#8217;t resist the temptation to write some production code without a test, above all if I have to implement some cross cutting behaviours, as could be logging . In project I worked on lately, a request was to manage some bad situations logging something at error [...]]]></description>
			<content:encoded><![CDATA[<p>Althought I&#8217;m a straight adept of Tdd, sometime I can&#8217;t resist the temptation to write some production code without a test, above all if I have to implement some cross cutting behaviours, as could be logging .</p>
<p>In project I worked on lately, a request was to manage some bad situations logging something at error level and go process next item; the temptation to implement it and check running the program was very appeliling, but my green wrist band and RailsConf 09 keynote by Uncle Bob I watched recentely, helped me resist.<br />
So I started searching, tried somenthing, and, finally, I came up with a neat solution.</p>
<p>Given this simple object</p>
<pre class='brush: java'>
package biz.scalzo.prod;

import org.apache.log4j.Logger;

public class LoggingObject {
	private static Logger log = Logger.getLogger(LoggingObject.class);

	public void starts() {
		log.info("I'm starting");
	}
}
</pre>
<p>we want to test if it writes log.<br />
To achieve this, we can use a simple custom appender, that records all messages in a list:</p>
<pre class='brush: java'>
package biz.scalzo.test;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

public class RecordingAppender extends AppenderSkeleton {
	private static List&lt;String> messages = new ArrayList&lt;String>();
	private static RecordingAppender appender = new RecordingAppender();

	private RecordingAppender() {
		super();
	}

	public static RecordingAppender appender(PatternLayout patternLayout) {
		appender.setLayout(patternLayout);
		appender.clear();
		return appender;
	}

	protected void append(LoggingEvent event) {
		messages.add(layout.format(event));
	}

	public void close() {
	}

	public boolean requiresLayout() {
		return true;
	}

	public static String[] messages() {
		return (String[]) messages.toArray(new String[messages.size()]);
	}

	private void clear() {
		messages.clear();
	}
}
</pre>
<p>With that, writing our test is a piece of cake:</p>
<pre class='brush: java'>
package biz.scalzo.test;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import biz.scalzo.prod.LoggingObject;

public class ALoggingObject {

	private LoggingObject loggingObj;

	@Before
	public void setUp() {
		loggingObj = new LoggingObject();
		configureLog();
	}

	private boolean logContains(String expected) {
		String actual[] = RecordingAppender.messages();
		for (String log : actual) {
			if (log.contains(expected))
				return true;
		}
		return false;
	}

	private void configureLog() {
		Logger rootLogger = Logger.getRootLogger();
		rootLogger.removeAllAppenders();
		rootLogger.setLevel(Level.INFO);
		rootLogger.addAppender(new ConsoleAppender(new PatternLayout(
				"%d [%t] %-5p %c{1} - %m%n")));
		rootLogger.addAppender(RecordingAppender.appender(new PatternLayout(
				"%-5p - %m%n")));
	}

	@Test
	public void shouldLogWhenStarts() {
		loggingObj.starts();
		assertTrue(logContains("I'm starting"));
	}

}
</pre>
<p>Neat, simple and clear: no more excuses to not test logging!</p>

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

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

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/10/21/no-more-excuses-junit-testing-log-messages/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Infinitest: an autotest for Eclipse</title>
		<link>http://giordano.scalzo.biz/2009/09/09/infinitest-an-autotest-for-eclipse/</link>
		<comments>http://giordano.scalzo.biz/2009/09/09/infinitest-an-autotest-for-eclipse/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 17:00:07 +0000</pubDate>
		<dc:creator>giordano scalzo</dc:creator>
				<category><![CDATA[agile]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[infinitest]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://giordano.scalzo.biz/?p=70</guid>
		<description><![CDATA[In my experimental trips in Ruby world, other than the elegance of the language, the Bdd as default and the lot of new exciting things created by the community, a thing I liked a lot was autotest , a little gem that launches our testsuite when we change a source file. It could seem a [...]]]></description>
			<content:encoded><![CDATA[<p>In my experimental trips in Ruby world, other than the elegance of the language, the Bdd as default and the lot of new exciting things created by the community, a thing I liked a lot was <a href="http://www.zenspider.com/ZSS/Products/ZenTest/" target="_blank">autotest</a> , a little gem that launches our testsuite when we change a source file.<br />
It could seem a silly utility, but I can assure it&#8217;s a really time saver and can take us in a high productive mental flow: I really missed it in Java world.</p>
<p>Of course, I&#8217;ve heard about <a href="http://www.threeriversinstitute.org/junitmax/subscribe.html" target="_blank">Junit Max</a>, the Kent Beck&#8217;s attempt to create a product, but my laziness delayed me to try it, until Kent got bored and dismiss the project <img src='http://giordano.scalzo.biz/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> .</p>
<p>Today while reading distractedly the Twitterverse, I bumped into <a href="http://infinitest.org" target="_blank">Infinitest</a>, that seems exactly what I was looking for.<br />
<a href="http://infinitest.org" target="_blank">Infinitest</a> isn&#8217;t open source, but for personal use it should be possible get a free of charge <a href="http://improvingworks.com/products/infinitest/for-individuals/" target="_blank">individual license</a>.</p>
<p>The installation is straightforward as usual, just add <code>http://update.improvingworks.com </code>to Eclipse&#8217;s software updates, and we are ready to start!</p>
<p><a href="http://infinitest.org" target="_blank">Infinitest</a> stay in the right bottom of Eclipse, waiting far some changes:</p>
<p><img class="aligncenter size-full wp-image-71" title="waiting for changes" src="http://giordano.scalzo.biz/wp-content/uploads/2009/09/waiting.png" alt="waiting for changes" width="441" height="43" /></p>
<p>When we save some file <a href="http://infinitest.org" target="_blank">Infinitest</a> starts and show the results of all test it founds in the project:</p>
<p><img class="aligncenter size-full wp-image-74" title="red test" src="http://giordano.scalzo.biz/wp-content/uploads/2009/09/red.png" alt="red test" width="507" height="199" /></p>
<p>A nice feature is the yellow bar when there is some error in the workspace and tests can&#8217;t run:</p>
<p><img class="aligncenter size-full wp-image-76" title="yellow bar" src="http://giordano.scalzo.biz/wp-content/uploads/2009/09/ytest.png" alt="yellow bar" width="716" height="359" /></p>
<p>That&#8217;s all!<br />
I have to try it more, but it seems very promising and useful.</p>

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

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/bdd' rel='tag' target='_self'>bdd</a>, <a class='technorati-link' href='http://technorati.com/tag/eclipse' rel='tag' target='_self'>eclipse</a>, <a class='technorati-link' href='http://technorati.com/tag/infinitest' rel='tag' target='_self'>infinitest</a>, <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/tdd' rel='tag' target='_self'>tdd</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://giordano.scalzo.biz/2009/09/09/infinitest-an-autotest-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

