<?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>Kai Ramuenke &#187; Mocks</title>
	<atom:link href="http://www.ramuenke.de/archives/tag/mocks/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ramuenke.de</link>
	<description>Just another agile software development blog</description>
	<lastBuildDate>Wed, 20 May 2009 13:28:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Direct field dependency injection</title>
		<link>http://www.ramuenke.de/archives/6</link>
		<comments>http://www.ramuenke.de/archives/6#comments</comments>
		<pubDate>Sat, 19 Jul 2008 12:30:02 +0000</pubDate>
		<dc:creator>Kai</dc:creator>
				<category><![CDATA[Agile Software Development]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Mocks]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.ramuenke.de/?p=6</guid>
		<description><![CDATA[At almost any project i have been on there was a discussion whether to use constructor or setter injection. Sometimes it tends to be a religious war between developers and either side has good arguments for both approaches. With constructor injection you make sure that all dependencies are available on creation time of the object. [...]]]></description>
			<content:encoded><![CDATA[<p>At almost any project i have been on there was a discussion whether to use constructor or setter injection. Sometimes it tends to be a religious war between developers and either side has good arguments for both approaches. With constructor injection you make sure that all dependencies are available on creation time of the object. Setter injection on the other hand gives you a little bit more flexibility. Especially if a class has more than 3 constructor parameters it&#8217;s getting a bit clunky with constructor injection.<br />
I personally tend more to constructor injection but since the auto-wiring capabilities of the Spring frameworks there is a third way to do your dependency injection: inject a dependency directly into the field of a class. Of course this approach has a downside if the class is used outside the Spring container, but for most Spring-based projects this is quite unlikely. Let&#8217;s look at some code to see how field injection works.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Service
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloService <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Hello! I'm a service&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeComponent <span style="color: #009900;">&#123;</span>
&nbsp;
    @Autowired
    <span style="color: #000000; font-weight: bold;">private</span> HelloService service<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> saySomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> service.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s as simple as that. The classes are annotated with <code>@Service</code> and <code>@Component</code> to be considered as candidates for auto-detection as i prefer to have as little XML configuration as possible. Thus the <code>applicationContext.xml</code> is quite simple:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xmlns:context</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/context&quot;</span></span>
<span style="color: #009900;">       <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans</span>
<span style="color: #009900;">           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd</span>
<span style="color: #009900;">           http://www.springframework.org/schema/context</span>
<span style="color: #009900;">           http://www.springframework.org/schema/context/spring-context-2.5.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:annotation-config</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;org.playground.di&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Of course we usually would start out by writing tests first. But how can we write unit tests for <code>SomeComponent</code> when we don&#8217;t have access to the field and inject a mock object? Luckily the developers of Spring thought of that and provide us with a class called <code>ReflectionTestUtils</code> which is available since Spring 2.5. For this unit test i&#8217;ll use <a href="http://code.google.com/p/mockito/">Mockito</a> to mock out <code>HelloService</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeComponentUnitTest <span style="color: #009900;">&#123;</span>
&nbsp;
    @Mock
    <span style="color: #000000; font-weight: bold;">private</span> HelloService mockService<span style="color: #339933;">;</span>
&nbsp;
    @Before
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initMocks<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MockitoAnnotations.<span style="color: #006633;">initMocks</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldUseMockService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        SomeComponent component <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SomeComponent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ReflectionTestUtils.<span style="color: #006633;">setField</span><span style="color: #009900;">&#40;</span>component, <span style="color: #0000ff;">&quot;service&quot;</span>, mockService<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> expectedMessage <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;I'm from a mock&quot;</span><span style="color: #339933;">;</span>
        Mockito.<span style="color: #006633;">stub</span><span style="color: #009900;">&#40;</span>mockService.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toReturn</span><span style="color: #009900;">&#40;</span>expectedMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> actualMessage <span style="color: #339933;">=</span> component.<span style="color: #006633;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        verify<span style="color: #009900;">&#40;</span>mockService<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sayHello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span>actualMessage, expectedMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To complete our testing we also gonna write an integration test:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration<span style="color: #009900;">&#40;</span>locations <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;classpath:applicationContext.xml&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeComponentIntegrationTest <span style="color: #009900;">&#123;</span>
&nbsp;
    @Autowired
    <span style="color: #000000; font-weight: bold;">private</span> SomeComponent consumer<span style="color: #339933;">;</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> shouldSaySomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> expectedMessage <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello! I'm a service&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> actualMessage <span style="color: #339933;">=</span> consumer.<span style="color: #006633;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #009900;">&#40;</span>expectedMessage, actualMessage<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.ramuenke.de/archives/6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
