<?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>Gustavo Peiretti &#187; Spring</title>
	<atom:link href="http://gustavopeiretti.com/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>http://gustavopeiretti.com</link>
	<description>Desarrollo de Software</description>
	<lastBuildDate>Fri, 20 Jan 2012 12:21:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Integrar JUnit con Spring</title>
		<link>http://gustavopeiretti.com/2009/integrar-junit-con-spring/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=integrar-junit-con-spring</link>
		<comments>http://gustavopeiretti.com/2009/integrar-junit-con-spring/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 14:01:30 +0000</pubDate>
		<dc:creator>Gustavo</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://gustavopeiretti.com/?p=813</guid>
		<description><![CDATA[Integrar JUnit con Spring es bastante simple. Solo debes crear tu test y anotarlo con @RunWith(SpringJUnit4ClassRunner.class). JUnit invocará la clase de referencia, en este caso &#8216;SpringJUnit4ClassRunner&#8217;. SpringJUnit4ClassRunner se encargará de levantar el contexto de Spring, en este ejemplo tengo dos contextos, donde el segundo &#8216;sobreescribe&#8217; al primero. La configuración del contexto puedes pasarla con otra &#8230; </p><p><a class="more-link block-button" href="http://gustavopeiretti.com/2009/integrar-junit-con-spring/">Continuar leyendo &#187;</a>]]></description>
			<content:encoded><![CDATA[<p>Integrar <a href="http://www.junit.org/">JUnit</a> con <a href="http://www.springsource.org/">Spring</a> es bastante simple. Solo debes crear tu test  y anotarlo con @RunWith(SpringJUnit4ClassRunner.class).</p>
<p>JUnit invocará la clase de referencia, en este caso &#8216;SpringJUnit4ClassRunner&#8217;.<br />
SpringJUnit4ClassRunner se encargará de levantar el contexto de Spring, en este ejemplo tengo dos contextos, donde el segundo &#8216;sobreescribe&#8217; al primero.</p>
<p>La configuración del contexto puedes pasarla con otra anotación de Spring @ContextConfiguration</p>
<p>Para inyectar un bean de Spring en el test deben marcarlo con @Autowired.</p>
<p>Spring buscará un bean definido con el mismo nombre del atributo y lo inyectará en tu test. Si queres forzar a Spring para que utilice un bean puedes utilizar @Qualifier</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.BasicConfigurator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Level</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.log4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.AfterClass</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Assert</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.BeforeClass</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Autowired</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.beans.factory.annotation.Qualifier</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * @author gustavo
 */</span>
@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;/gp/applicationContext.xml&quot;</span>, <span style="color: #0000ff;">&quot;/gp/applicationContextTesting.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> TestWithSpring <span style="color: #009900;">&#123;</span>
&nbsp;
    @BeforeClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> setup<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        BasicConfigurator.<span style="color: #006633;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Logger.<span style="color: #006633;">getRootLogger</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">setLevel</span><span style="color: #009900;">&#40;</span>Level.<span style="color: #006633;">INFO</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//... configuracion de inicio</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Autowired
    @Qualifier<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;_testSpringList&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">ArrayList</span> _testSpringList<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// spring inyectara este atributo</span>
&nbsp;
    @Test
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> simpleTest<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;">Assert</span>.<span style="color: #006633;">assertFalse</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>._testSpringList.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> s <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span>._testSpringList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>s.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>    
&nbsp;
    @AfterClass
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> endClazz<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//... </span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>En Spring tengo definido <strong>_testSpringList</strong> de esta manera:</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;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;_testSpringList&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;java.util.ArrayList&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>hi!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>bye!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://gustavopeiretti.com/2009/integrar-junit-con-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

