<?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>Gojko Adzic &#187; java</title>
	<atom:link href="http://gojko.net/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://gojko.net</link>
	<description>Building software that matters</description>
	<lastBuildDate>Tue, 22 May 2012 19:12:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Three cool Java TDD ideas from Oredev</title>
		<link>http://gojko.net/2010/11/15/three-cool-java-tdd-ideas-from-oredev/</link>
		<comments>http://gojko.net/2010/11/15/three-cool-java-tdd-ideas-from-oredev/#comments</comments>
		<pubDate>Mon, 15 Nov 2010 14:10:36 +0000</pubDate>
		<dc:creator>gojko</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jtestr]]></category>
		<category><![CDATA[mockrunner]]></category>
		<category><![CDATA[oredev]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unitils]]></category>

		<guid isPermaLink="false">http://gojko.net/?p=2107</guid>
		<description><![CDATA[Neal Ford presented several ideas, tools and libraries for more productive Java unit testing at Oredev 2010. Ford suggested going outside the box and looking at writing unit tests in other JVM languages even if the target language is Java. “Always test a weaker language with a stronger one”, said...]]></description>
			<content:encoded><![CDATA[<p><a href="http://memeagora.blogspot.com/">Neal Ford</a> presented several ideas, tools and libraries for more productive Java unit testing at <a href='/tag/oredev'>Oredev 2010</a>.<span id="more-2107"></span></p>
<p>Ford suggested going outside the box and looking at writing unit tests in other JVM languages even if the target language is Java. “Always test a weaker language with a stronger one”, said Ford, suggesting that people should use Groovy to write Java unit tests. According to him, Groovy makes writing unit tests easier because it ignores privates and exceptions. Another option is JRuby. “Ruby guys have the coolest testing tools in the world”, said Ford. Java developers can use those tools with the help of <a href="http://jtestr.codehaus.org/">JTestR</a>, which makes it easy to write Java unit tests in JRuby and use standard Ruby testing tools for Java projects. </p>
<p>For teams working with J2EE technologies, he suggested using <a href="http://mockrunner.sourceforge.net/">Mockrunner</a>, which “stubs out J2EE by magic”, according to Ford. Mockrunner has stubs that provide out of the box functionality for JMS, JDBC, Servlet and many other J2EE APIs. It should allow unit testing of code that depends on J2EE APIs outside a container.</p>
<p>For teams that work with the Spring stack and related APIs, he suggested using <a href="http://unitils.org">Unitils</a>, a “swiss army chainsaw of useful stuff”. It supports stubbing out Hibernate, Spring, JPA and similar APIs to speed up unit testing without having to really load the spring context or work against a database</p>
]]></content:encoded>
			<wfw:commentRss>http://gojko.net/2010/11/15/three-cool-java-tdd-ideas-from-oredev/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Secret Ninja Cucumber Scrolls</title>
		<link>http://gojko.net/2010/09/20/the-secret-ninja-cucumber-scrolls/</link>
		<comments>http://gojko.net/2010/09/20/the-secret-ninja-cucumber-scrolls/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 14:04:15 +0000</pubDate>
		<dc:creator>gojko</dc:creator>
				<category><![CDATA[news]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[agile testing]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[cuke4ninja]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://gojko.net/?p=1996</guid>
		<description><![CDATA[It is my great pleasure to announce the immediate availability of The Secret Ninja Cucumber Scrolls, a free e-book and online step-by-step guide for Cucumber, a tool that is quickly becoming the weapon of choice for many agile teams when it comes to functional test automation, creating executable specifications and...]]></description>
			<content:encoded><![CDATA[<p>It is my great pleasure to announce the immediate availability of <a href="http://cuke4ninja.com">The Secret Ninja Cucumber Scrolls</a>, a free e-book and online step-by-step guide for Cucumber, a tool that is quickly becoming the weapon of choice for many agile teams when it comes to functional test automation, creating executable specifications and building a living documentation. </p>
<p>The main author of this e-book is my colleague <a href="http://deflorinier.blogspot.com/">David de Florinier</a>, and I&#8217;ve also contributed a bit. </p>
<p>The first version covers the basics of Cucumber, setting up for Ruby, Java and .NET projects, implementing basic features in all three languages, effective test design and managing complex features efficiently. We plan to develop this guide incrementally, and the  next version will include web browser automation topics.    </p>
<p>To download the PDF or read the e-Book online, point your browser to <a href="http://cuke4ninja.com">cuke4ninja.com</a>. For news and updates, follow <a href="http://twitter.com/cuke4ninja">cuke4ninja</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://gojko.net/2010/09/20/the-secret-ninja-cucumber-scrolls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mockito in six easy examples</title>
		<link>http://gojko.net/2009/10/23/mockito-in-six-easy-examples/</link>
		<comments>http://gojko.net/2009/10/23/mockito-in-six-easy-examples/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 13:41:39 +0000</pubDate>
		<dc:creator>gojko</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[tdd]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://gojko.net/?p=1346</guid>
		<description><![CDATA[Mockito is a fantastic mock library for Java. I&#8217;m fascinated by how easy it is to use, compared to other things out there both in the Java and .NET world. Here is everything you need to know to get started in six really easy examples. First of all, get mockito...]]></description>
			<content:encoded><![CDATA[<p>Mockito is a fantastic mock library for Java. I&#8217;m fascinated by how easy it is to use, compared to other things out there both in the Java and .NET world. Here is everything you need to know to get started in six really easy examples.<span id="more-1346"></span></p>
<p>First of all, get mockito from <a href="http://mockito.org">http://mockito.org/</a>. Almost everything really interesting can be imported with the org.mockito.Mockito class (or a static import of its methods, which I&#8217;ll use in this post). So let&#8217;s get right into it. </p>
<p>To create a stub (or a mock), use mock(class). Then use when(mock).thenReturn(value) to specify the stub value for a method. If you specify more than one value, they will be returned in sequence until the last one is used, after which point the last specified value gets returned. (So to have a method return the same value always, just specify it once). For example:</p>
<pre style="margin-bottom:10px">
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Test;
....
	@Test
	public void iterator_will_return_hello_world(){
		//arrange
		Iterator i=mock(Iterator.class);
		when(i.next()).thenReturn("Hello").thenReturn("World");
		//act
		String result=i.next()+" "+i.next();
		//assert
		assertEquals("Hello World", result);
	}
</pre>
<p>This example creates a mock iterator and makes it return &#8220;Hello&#8221; the first time method next() is called. Calls after that return &#8220;World&#8221;. Then we can run normal assertions. </p>
<p>Stubs can also return different values depending on arguments passed into the method. For example:</p>
<pre style="margin-bottom:10px">
	@Test
	public void with_arguments(){
		Comparable c=mock(Comparable.class);
		when(c.compareTo("Test")).thenReturn(1);
		assertEquals(1,c.compareTo("Test"));
	}
</pre>
<p>This creates a stub Comparable object and returns 1 if it is compared to a particular String value (&#8220;Test&#8221; in this case). If the method has arguments but you really don&#8217;t care what gets passed or cannot predict it, use anyInt() (and alternative values for other types). For example:</p>
<pre style="margin-bottom:10px">
	@Test
	public void with_unspecified_arguments(){
		Comparable c=mock(Comparable.class);
		when(c.compareTo(anyInt())).thenReturn(-1);
		assertEquals(-1,c.compareTo(5));
	}
</pre>
<p>This stub comparable returns -1 regardless of the actual method argument. With void methods, this gets a bit tricky as you can&#8217;t use them in the when() call. The alternative syntax is doReturn(result).when(mock_object).void_method_call(); Instead of returning, you can also use .thenThrow() or doThrow() for void methods. For example:</p>
<pre style="margin-bottom:10px">
	@Test(expected=IOException.class)
	public void OutputStreamWriter_rethrows_an_exception_from_OutputStream()
		throws IOException{
		OutputStream mock=mock(OutputStream.class);
		OutputStreamWriter osw=new OutputStreamWriter(mock);
		doThrow(new IOException()).when(mock).close();
		osw.close();
	}
</pre>
<p>This example throws an IOException when the mock OutputStream close method is called. We verify easily that the OutputStreamWriter rethrows the exception of the wrapped output stream. To verify actual calls to underlying objects (typical mock object usage), we can use verify(mock_object).method_call; For example:</p>
<pre style="margin-bottom:10px">
	@Test
	public void OutputStreamWriter_Closes_OutputStream_on_Close()
		 throws IOException{
		OutputStream mock=mock(OutputStream.class);
		OutputStreamWriter osw=new OutputStreamWriter(mock);
		osw.close();
		verify(mock).close();
	}
</pre>
<p>This example will verify that OutputStreamWriter propagates the close method call to the wrapped output stream. You can use arguments on methods and matchers such as anyInt() similar to the previous example. Note that you can&#8217;t mix literals and matchers, so if you have multiple arguments they all have to be either literals or matchers. use eq(value) matcher to convert a literal into a matcher that compares on value. Mockito comes with lots of matchers already built in, but sometimes you need a bit more flexibility. For example, OutputStreamWriter will buffer output and then send it to the wrapped object when flushed, but we don&#8217;t know how big the buffer is upfront. So we can&#8217;t use equality matching. However, we can supply our own matcher:</p>
<pre style="margin-bottom:10px">
	@Test
	public void OutputStreamWriter_Buffers_And_Forwards_To_OutputStream()
		throws IOException{
		OutputStream mock=mock(OutputStream.class);
		OutputStreamWriter osw=new OutputStreamWriter(mock);
		osw.write('a');
		osw.flush();
		// can't do this as we don't know how long the array is going to be
		// verify(mock).write(new byte[]{'a'},0,1);

		BaseMatcher<byte []> arrayStartingWithA=new BaseMatcher</byte><byte []>(){
			@Override
			public void describeTo(Description description) {
				// nothing
			}
			// check that first character is A
			@Override
			public boolean matches(Object item) {
				byte[] actual=(byte[]) item;
				return actual[0]=='a';
			}
		};
		// check that first character of the array is A, and that the other two arguments are 0 and 1
		verify(mock).write(argThat(arrayStartingWithA), eq(0),eq(1));
	}
</byte></pre>
<p>That&#8217;s it &#8211; all you need to know to get started. Now go forth and refactor all that easymock ugliness from your projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://gojko.net/2009/10/23/mockito-in-six-easy-examples/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Announcing Trinidad: In-process test runner for FitNesse wiki pages</title>
		<link>http://gojko.net/2009/01/26/announcing-trinidad-in-process-test-runner-for-fitnesse-wiki-pages/</link>
		<comments>http://gojko.net/2009/01/26/announcing-trinidad-in-process-test-runner-for-fitnesse-wiki-pages/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 17:22:18 +0000</pubDate>
		<dc:creator>gojko</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[fitnesse]]></category>
		<category><![CDATA[acceptance testing]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[trinidad]]></category>

		<guid isPermaLink="false">http://gojko.net/2009/01/26/announcing-trinidad-in-process-test-runner-for-fitnesse-wiki-pages/</guid>
		<description><![CDATA[The first public beta release of Trinidad is now available. Trinidad is an in-process test runner for FitNesse tests. Here are some nice things you can do with it: run fitnesse fit and slim tests without starting the server as part of your build run fitnesse tests from JUnit within...]]></description>
			<content:encoded><![CDATA[<p>The first public beta release of Trinidad is now available.  Trinidad is an in-process test runner for FitNesse tests. Here are some nice things you can do with it:</p>
<ul>
<li>run fitnesse fit and slim tests without starting the server as part of your build</li>
<li>run fitnesse tests from JUnit within your IDE</li>
<li>debug and troubleshoot fitnesse fixtures as if you were working with unit tests</li>
<li>easily include fitnesse acceptance tests into your build (through JUnit)</li>
<li>easily run tests in transactions and roll back after each test (with Spring)</li>
</ul>
<p>For example, here is how you can run an entire FitNesse test suite from JUnit</p>
<pre>
public class JUnitExampleFitTest {
	JUnitHelper helper;
	@Before
	public void initHelper() throws Exception{
		helper=new JUnitHelper(new TestRunner(
				new FitNesseRepository("target/test-classes"),
				new FitTestEngine(),
				"/tmp/test-output"));
	}
	@Test
	public void runSuite() throws Exception{
		helper.assertSuitePasses("JavaExamples");
	}
	@Test
	public void runSingleTest() throws Exception{
		helper.assertTestPasses(
                   "JavaExamples.CommonExamples.SetUpFixture");
	}
}
</pre>
<p>At the moment, Trinidad only supports Java FIT/Slim, but .NET is in plan soon. For download links, usage instructions and examples, see <a href="http://fitnesse.info/trinidad">http://fitnesse.info/trinidad</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://gojko.net/2009/01/26/announcing-trinidad-in-process-test-runner-for-fitnesse-wiki-pages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Concordion: Agile Acceptance Testing with free-form text</title>
		<link>http://gojko.net/2008/08/25/concordion-agile-acceptance-testing-with-free-form-text/</link>
		<comments>http://gojko.net/2008/08/25/concordion-agile-acceptance-testing-with-free-form-text/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 15:42:12 +0000</pubDate>
		<dc:creator>gojko</dc:creator>
				<category><![CDATA[articles]]></category>
		<category><![CDATA[acceptance testing]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[concordion]]></category>
		<category><![CDATA[fit]]></category>
		<category><![CDATA[fitnesse]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://gojko.net/?p=302</guid>
		<description><![CDATA[I finally had some time to take a look at Concordion, an acceptance testing tool that I&#8217;ve heard about on several conferences. Concordion is an interesting alternative to FIT. It is developed by David Peterson and released under the Apache opensource license. Similar to FIT, Concordion uses HTML documents as...]]></description>
			<content:encoded><![CDATA[<p>I finally had some time to take a look at Concordion, an acceptance testing tool that I&#8217;ve heard about on several conferences. <a href="http://www.concordion.org" target="_blank">Concordion</a> is an interesting alternative to FIT. It is developed by <a href="http://www.davidpeterson.co.uk/" target="_blank">David Peterson</a> and released under the Apache opensource license. Similar to FIT, Concordion uses HTML documents as an executable specification and requires some glue code (fixtures) to connect the executable elements of that specification to the domain code. Unlike FIT, Concordion does not require the specification to be in any particular format &mdash; you can write examples as normal sentences, without any restrictions. <span id="more-302"></span></p>
<p>Concordion is really simple. Its instrumentation only allows programmers to set global test variables, execute fixture methods and compare actual results with expected values. Programmers can use special HTML element attributes to mark words or phrases that are used as test inputs or compared to test results. Web browsers will just ignore unknown element attributes, so Concordion test instrumentation is effectively invisible to people that are not interested in test automation. For example, here is a HTML document that I&#8217;ve used as a simple test:</p>
<div style="margin-bottom:10px">
<pre>
&lt;html xmlns:concordion="http://www.concordion.org/2007/concordion"&gt;
&lt;body&gt;
&lt;h1&gt;Free delivery&lt;/h1&gt;
&lt;ul&gt;

 &lt;li concordion:execute="#offer = checkFreeDelivery(#type, #books)"&gt;
 Free delivery &lt;span concordion:assertEquals="#offer"&gt;is&lt;/span&gt;
 offered to a &lt;b concordion:set="#type"&gt;VIP&lt;/b&gt;
 customer with &lt;b concordion:set="#books"&gt;10&lt;/b&gt; books in the cart.
 &lt;/li&gt;

 &lt;li concordion:execute="#offer = checkFreeDelivery(#type, #books)"&gt;
  It &lt;b concordion:assertEquals="#offer"&gt;is not&lt;/b&gt; offered to
  a &lt;b concordion:set="#type"&gt;regular&lt;/b&gt; customer
  with &lt;b concordion:set="#books"&gt;10&lt;/b&gt; books&lt;/li&gt;

 &lt;li concordion:execute="#offer = checkFreeDelivery(#type, #books)"&gt;
  It &lt;b concordion:assertEquals="#offer"&gt;is not&lt;/b&gt; offered to
  a &lt;b concordion:set="#type"&gt;VIP&lt;/b&gt; customer with
  only &lt;b concordion:set="#books"&gt;9&lt;/b&gt; books.&lt;/li&gt;

&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
<p>The <i>concordion:execute</i> command on the LI element specifies that the <i>offer</i> variable is set by executing the <i>checkFreeDelivery</i> method and passing <i>type</i> and <i>books</i> variables set in the element text. The <i>concordion:set</i> command is used to set a variable based on the inner text in the element. The <i>concordion:assertEquals</i> command inspects the inner text in the element and compares that with the result of a method or current variable value. For repetitive specifications and calculation rules, Concordion also supports attributes for tables similar to the FIT ColumnFixture. </p>
<p>At the moment, Concordion only supports Java fixtures &mdash; It actually works as a JUnit extension. This provides direct integration with JUnit test runners, making it much easier to execute Concordion tests from popular development environments and integrate into continuous build systems. It also shortens the learning curve required to start using Concordion. The fixture is simply a JUnit test class. It should be called the same as the HTML file (in this case, the file was ConcordionTest.html, so the class is ConcordionTest.java), and declare the methods that are used by <i>concordion:execute</i>. </p>
<div style="margin-bottom:10px">
<pre>
package conctest;

import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;

@RunWith(ConcordionRunner.class)

public class ConcordionTest {
	public boolean freeDelivery(String type, int books){
			if (books&lt;9) return false;
			if (!"VIP".equals(type)) return false;
			return true;
	}
	public String checkFreeDelivery(String type, int books){
		return freeDelivery(type, books)?"is":"is not";
	}
}
</pre>
</div>
<p>My first impression of this is that the fixture model of Concordion does not depend on inheritance so it is a lot simpler to learn than FIT and somewhat more flexible to write Concordion than FIT fixtures. On the other hand, Concordion lacks the extensibility and powerful model of type adapters and cell handlers that enable FIT to bind domain objects and business services directly to the specification. </p>
<p>When the JUnit test is executed, Concordion runs through the files and executes commands, verifying expected outputs with actual results in <i>assertEquals</i>. JUnit test run will tell you whether all tests passed or were there failures, and Concordion also saves the results in the system temporary folder in HTML form, making it easier to see what actually went wrong. The example above intentionally has an error &mdash; here is the screenshot of the result:</p>
<p><img src="/images/concordion.png" style="border:1px solid black" />  </p>
<p>Concordion does not have a test management tool and relies completely on the programmer&#8217;s IDE to manipulate and execute tests. I have mixed feelings about this. Although file management within an IDE makes it much easier to put tests in the same version control system as the domain code, I miss the ability to re-use parts of the test specification such as common set-ups or test components with macro variables that are available in FitNesse. It also puts tests completely under the control of programmers.</p>
<p>Concordion takes some ideas that have evolved as best practices to use FIT/FitNesse and makes them very explicit, actively discouraging other ways of working. For example, acceptance tests have to be stored in the same version control system as the code, there are no pre-built test building blocks that would encourage scripting. On the other hand, while FIT/FitNesse community is moving firmly towards using domain objects directly in acceptance tests and reducing the amount of glue code, to eliminate translation between layers and promote domain-driven design, Concordion model is stuck in requiring you to create an explicit test method for every verification in the fixture.</p>
<p>The thing that really worries me is the fact that HTML files are stored next to Java classes and that developers need to add non-standard HTML attributes to the test page. This smells to me of a hand-over of tests from business people to developers at some point, which is a practice that I don&#8217;t approve at all. In my opinion, tests should be shared between the whole team and not handed-over down the pipeline. I guess that this can be avoided with some discipline and research into tools that will surely not overwrite non-standard HTML attributes. But I would still like to see a proper test management tool for business people to use.</p>
<p>I&#8217;ve been hearing a lot about Concordion recently, mostly being mentioned by people as an interesting tool to look into. Safari has no books about it, all I could find on Google and Technorati blog searches are reports of people coming across the tool and doing some basic things with it, such as this post. I would be really interested in hearing stories from people that have real-world experiences with this tool to share. What happens six months later &mdash; are tests easier to manage than with FitNesse, has it turned out to be good enough tool to talk to business people? Is my fear of hand-over real, or is it not a problem at all? </p>
]]></content:encoded>
			<wfw:commentRss>http://gojko.net/2008/08/25/concordion-agile-acceptance-testing-with-free-form-text/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

