<?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; JUnit</title>
	<atom:link href="http://gustavopeiretti.com/tag/junit/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>
		<item>
		<title>Mejora la calidad de tu software con testing</title>
		<link>http://gustavopeiretti.com/2009/mejora-calidad-software-testing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mejora-calidad-software-testing</link>
		<comments>http://gustavopeiretti.com/2009/mejora-calidad-software-testing/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 02:13:28 +0000</pubDate>
		<dc:creator>Gustavo</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Tecnología]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Util]]></category>

		<guid isPermaLink="false">http://gustavopeiretti.com/?p=638</guid>
		<description><![CDATA[Mejorar la calidad de tu software depende también de que hagas buenos test durante su desarrollo. Es importante tener presente en el ciclo de desarrollo las pruebas y orientarse hacia el testing para evitarse futuros e innecesarios problemas. Los test te ayudarán a encontrar errores, pero ten presente que aunque el test no falle, esto &#8230; </p><p><a class="more-link block-button" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/">Continuar leyendo &#187;</a>]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-662" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/testing_soft/"><img class="alignleft size-full wp-image-662" title="Testing" src="http://gustavopeiretti.com/blog/wp-content/uploads/2009/08/testing_soft.png" alt="Testing" width="128" height="128" /></a>Mejorar la calidad de tu software depende también de que hagas buenos test durante su desarrollo.</p>
<p>Es importante tener presente en el ciclo de desarrollo las pruebas y orientarse hacia el testing para evitarse futuros e innecesarios problemas. Los test te ayudarán a encontrar errores, pero ten presente que aunque el test no falle, esto no significa que el software no contenga errores.<br />
Como dijo ‘Dijkstra’ “…El testing prueba la presencia de errores pero no la ausencia de ellos…&#8221;</p>
<p>Hay que distinguir dos tipos de errores: los llamados errores de software o bugs y los errores de forma.<br />
Los errores de forma son aquellos en los cuales el software no falla en su código pero si en su funcionalidad, es decir no cumple con lo que se espera de ellos como resultado.</p>
<p>Los frameworks de testing como JUnit, TestNG te ayudarán principalmente con los bugs y otro poco con los errores de funcionalidad, pero estos últimos deben ser subsanados con un buen ciclo de entregas y retroalimentación continuo del software.<br />
Además, estos frameworks te serán de gran utilidad en entornos de aplicación ‘pesados’ donde probar el software significa, por ejemplo, levantar un servidor web.</p>
<p>Otro punto positivo, es que si diseñas buenas suites de pruebas, podrás  luego,  ante un cambio por mantenimiento en tu software,  realizar una prueba integral de todos los módulos del mismo verificando que no hayas ‘roto’ alguna otra cosa.<br />
Testear el software es poco divertido (al menos para mí) y apoyarse en un buen framework de testing ayuda a aliviar en parte esa tediosa (pero necesaria) tarea de probar el software. Una vez que tienes hecho el test, lo ejecutas las veces que quieras.</p>
<p>JUnit es un clásico de los test, muy conocido y muy integrado a los principales entornos de desarrollo.<br />
TestNG es otra buena opción para realizar tus pruebas.</p>
<p>Veamos un poco de JUnit con un simple ejemplo:</p>
<h3><a href="http://www.junit.org/">JUnit</a></h3>
<p style="text-align: center;"><img src="file:///C:/DOCUME%7E1/gustavo/CONFIG%7E1/Temp/moz-screenshot-4.png" alt="" /><a rel="attachment wp-att-639" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/junit-run/"><img class="size-full wp-image-639 aligncenter" title="junit-run" src="http://gustavopeiretti.com/blog/wp-content/uploads/2009/08/junit-run.png" alt="junit-run" width="525" height="53" /></a></p>
<p style="text-align: left;">Esta es la consola de JUnit<br />
<a rel="attachment wp-att-640" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/junit-console/"><img class="size-full wp-image-640 aligncenter" title="junit-console" src="http://gustavopeiretti.com/blog/wp-content/uploads/2009/08/junit-console.png" alt="junit-console" width="571" height="88" /></a></p>
<p>Anotaciones mas usadas:</p>
<p>@BeforeClass:  se ejecuta una sola vez para todos los test. Util para inicializar variables u otros datos que serán compartidos.<br />
@AfterClass: se ejecuta una sola vez al finalizar los test.<br />
@Before: se ejecuta previo al iniciar cada @Test.<br />
@After: se ejecuta luego de cada @Test<br />
@Test: metodo que será ejecutado como test.</p>
<p>Nuestra clase de test JUnit…<br />
&#8212;&#8212;-</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">After</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Assert</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Before</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Ignore</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Test</span><span style="color:#800080; ">;</span>

<span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#800000; font-weight:bold; ">class</span> SimpleTestJUnit <span style="color:#800080; ">{</span>

    Calculator calc<span style="color:#800080; ">;</span>

    <span style="color:#808030; ">@</span>Before
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> setUp<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// Before indica que debe ejecutrarse antes de iniciar el test...</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.setUp ..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        calc <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">new</span> Calculator<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>After
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> after<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// After indica que debe ejecutrarse despues del test...</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.after..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> testDivision<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.testDivision..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span>calc<span style="color:#808030; ">.</span>getDivision<span style="color:#808030; ">(</span><span style="color:#008c00; ">10</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">3</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> testDivisionByZero<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// este test se ejecutara con error</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.testDivisionByZero..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span>calc<span style="color:#808030; ">.</span>getDivision<span style="color:#808030; ">(</span><span style="color:#008c00; ">10</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">0</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test<span style="color:#808030; ">(</span>expected <span style="color:#808030; ">=</span> <span style="color:#bb7977; font-weight:bold; ">ArithmeticException</span><span style="color:#808030; ">.</span>class<span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> testDivisionByZeroWithExpectedException<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// Este test no dara error. Esperamos la exception ArithmeticException</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.testDivisionByZeroWithExpectedException..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span>calc<span style="color:#808030; ">.</span>getDivision<span style="color:#808030; ">(</span><span style="color:#008c00; ">10</span><span style="color:#808030; ">,</span> <span style="color:#008c00; ">0</span><span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> testPrimeNumber<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.testPrimeNumber..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; ">int</span> i <span style="color:#808030; ">=</span> <span style="color:#008c00; ">10</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; ">int</span> j <span style="color:#808030; ">=</span> <span style="color:#008c00; ">5</span><span style="color:#800080; ">;</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Es numero primo: "</span> <span style="color:#808030; ">+</span> calc<span style="color:#808030; ">.</span>isPrimeNumber<span style="color:#808030; ">(</span>i<span style="color:#808030; ">,</span> j<span style="color:#808030; ">)</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        Assert<span style="color:#808030; ">.</span>assertTrue<span style="color:#808030; ">(</span>calc<span style="color:#808030; ">.</span>getRest<span style="color:#808030; ">(</span>i<span style="color:#808030; ">,</span> j<span style="color:#808030; ">)</span> <span style="color:#808030; ">=</span><span style="color:#808030; ">=</span> <span style="color:#008c00; ">0</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Ignore
    <span style="color:#808030; ">@</span>Test
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> testIgnore<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// NO se ejecutara debido a que tiene la anotacion @Ignore...</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"SimpleTest.testIgnore..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

<span style="color:#800080; ">}</span></pre>
<p>&#8212;&#8212;&#8212;&#8211;<br />
Nuestra clase Calculator que usamos para este ejemplo es&#8230;<br />
&#8211;</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#800000; font-weight:bold; ">class</span> Calculator <span style="color:#800080; ">{</span>

    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">double</span> getDivision<span style="color:#808030; ">(</span><span style="color:#bb7977; ">int</span> v1<span style="color:#808030; ">,</span> <span style="color:#bb7977; ">int</span> v2<span style="color:#808030; ">)</span><span style="color:#800080; ">{</span>
        <span style="color:#bb7977; ">int</span> i <span style="color:#808030; ">=</span> v1 <span style="color:#808030; ">/</span> v2 <span style="color:#800080; ">;</span>
        <span style="color:#800000; font-weight:bold; ">return</span> i<span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">double</span> getRest<span style="color:#808030; ">(</span><span style="color:#bb7977; ">int</span> v1<span style="color:#808030; ">,</span> <span style="color:#bb7977; ">int</span> v2<span style="color:#808030; ">)</span><span style="color:#800080; ">{</span>
        <span style="color:#bb7977; ">double</span> i <span style="color:#808030; ">=</span> v1 <span style="color:#808030; ">%</span> v2<span style="color:#800080; ">;</span>
        <span style="color:#800000; font-weight:bold; ">return</span> i<span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">boolean</span> isPrimeNumber<span style="color:#808030; ">(</span><span style="color:#bb7977; ">int</span> v1<span style="color:#808030; ">,</span> <span style="color:#bb7977; ">int</span> v2<span style="color:#808030; ">)</span><span style="color:#800080; ">{</span>
        <span style="color:#800000; font-weight:bold; ">return</span> getRest<span style="color:#808030; ">(</span>v1<span style="color:#808030; ">,</span> v2<span style="color:#808030; ">)</span> <span style="color:#808030; ">=</span><span style="color:#808030; ">=</span> <span style="color:#008c00; ">0</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

<span style="color:#800080; ">}</span></pre>
<p>&#8211;<br />
Ahora armaremos un suite para ejecutar varios test’s juntos…<br />
Defines una clase con las dos anotaciones que se muestran, en una de ellas ‘SuiteClasses’ indicarás los test a ejecutar.</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">runner</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">RunWith</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">runners</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Suite</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">junit</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">runners</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Suite</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">SuiteClasses</span><span style="color:#800080; ">;</span>

@RunWith(Suite.class)
@SuiteClasses( <span style="color:#800080; ">{</span> SimpleTestJUnit<span style="color:#808030; ">.</span>class<span style="color:#808030; ">,</span> SimpleTestJUnit2<span style="color:#808030; ">.</span>class<span style="color:#800080; ">}</span>)
<span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#800000; font-weight:bold; ">class</span> AllTests <span style="color:#800080; ">{</span>

    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#800000; font-weight:bold; ">static</span> <span style="color:#bb7977; ">void</span> main<span style="color:#808030; ">(</span><span style="color:#bb7977; font-weight:bold; ">String</span><span style="color:#808030; ">[</span><span style="color:#808030; ">]</span> args<span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// esta clase ejecutara los test's definidos en @SuiteClasses</span>
    <span style="color:#800080; ">}</span>

<span style="color:#800080; ">}</span></pre>
<p>&#8212;&#8212;</p>
<h3><a href="http://testng.org/doc/documentation-main.html">TestNG</a></h3>
<p>Si conoces JUnit te resultará mas que fácil usarlo y si no, verás que es muy parecido a JUnit<br />
Este framework se integra a Eclipse con su plug-in</p>
<p style="text-align: center;"><a rel="attachment wp-att-641" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/testng-run/"><img class="size-full wp-image-641 aligncenter" title="testng-run" src="http://gustavopeiretti.com/blog/wp-content/uploads/2009/08/testng-run.png" alt="testng-run" width="528" height="95" /></a></p>
<p>Esta es la consola de TestNG (muy similar a la de JUnit)</p>
<p style="text-align: center;"><a rel="attachment wp-att-642" href="http://gustavopeiretti.com/2009/mejora-calidad-software-testing/testng-console/"><img class="size-full wp-image-642 aligncenter" title="testng-console" src="http://gustavopeiretti.com/blog/wp-content/uploads/2009/08/testng-console.png" alt="testng-console" width="571" height="150" /></a></p>
<p><a onmousedown="if(document.getElementById('divTestNG').style.display == 'none'){ document.getElementById('divTestNG').style.display = 'block'; }else{ document.getElementById('divTestNG').style.display = 'none'; }" href="javascript:;"><br />
Haz click aqui para ver un test simple con TestNG …<br />
</a></p>
<div id="divTestNG" style="display:none">&#8212;</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">testng</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">annotations</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">AfterTest</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">testng</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">annotations</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">BeforeTest</span><span style="color:#800080; ">;</span>
<span style="color:#800000; font-weight:bold; ">import</span><span style="color:#004a43; "> org</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">testng</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">annotations</span><span style="color:#808030; ">.</span><span style="color:#004a43; ">Test</span><span style="color:#800080; ">;</span>

<span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#800000; font-weight:bold; ">class</span> SimpleTestNG <span style="color:#800080; ">{</span>

    <span style="color:#808030; ">@</span>BeforeTest<span style="color:#808030; ">(</span>dependsOnMethods <span style="color:#808030; ">=</span> <span style="color:#0000e6; ">"setup"</span><span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> before<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// BeforeTest indica que debe ejecutarrse antes de iniciar los test. </span>
        <span style="color:#696969; ">// Antes se ejecutara 'setup' debido a que esta indicado con 'dependsOnMethods'</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.before (antes debio ejecutarse setup)..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>BeforeTest
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> setup<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span><span style="color:#800080; ">{</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.setup..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>AfterTest
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> after<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// AfterTest indica que debe ejecutrarse despues. </span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.after..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>    

    <span style="color:#808030; ">@</span>Test
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> test<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// este es el test a ejecutarse...</span>
        <span style="color:#696969; ">//... tu logica y llamadas a las clases que hace a la logica de negocio...</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.test..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test<span style="color:#808030; ">(</span>expectedExceptions<span style="color:#808030; ">=</span><span style="color:#800080; ">{</span><span style="color:#bb7977; font-weight:bold; ">RuntimeException</span><span style="color:#808030; ">.</span>class<span style="color:#800080; ">}</span><span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> test2<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.test2..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
        <span style="color:#696969; ">//Se espera que este test tire una expcetion 'RuntimeException'</span>
        <span style="color:#696969; ">//Si no ocurre la exception el test sera fallido</span>
        <span style="color:#696969; ">//throw new RuntimeException("Error esperado"); // si se comenta fallara el test </span>
    <span style="color:#800080; ">}</span>

    <span style="color:#808030; ">@</span>Test<span style="color:#808030; ">(</span>enabled <span style="color:#808030; ">=</span> <span style="color:#800000; font-weight:bold; ">false</span><span style="color:#808030; ">)</span>
    <span style="color:#800000; font-weight:bold; ">public</span> <span style="color:#bb7977; ">void</span> test3<span style="color:#808030; ">(</span><span style="color:#808030; ">)</span> <span style="color:#800080; ">{</span>
        <span style="color:#696969; ">// NO se ejecutara debido a que 'enabled = false'</span>
        <span style="color:#bb7977; font-weight:bold; ">System</span><span style="color:#808030; ">.</span>out<span style="color:#808030; ">.</span>println<span style="color:#808030; ">(</span><span style="color:#0000e6; ">"Ejecutando SimpleTest.test3..."</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>
    <span style="color:#800080; ">}</span>

<span style="color:#800080; ">}</span></pre>
<p>&#8212;</p></div>
<p>En TestNG podemos crear también una suite de test’s.<br />
Este xml ejecutará dos test SimpleTestNG, y SimpleTestNG2.<br />
Solo debes ejecutarlo con el menú de TestNG</p>
<pre style="color:#000000;background:#ffffff;"><span style="color:#004a43; ">&lt;!</span><span style="color:#004a43; ">DOCTYPE</span> <span style="color:#004a43; ">suite</span> <span style="color:#800000; font-weight:bold; ">SYSTEM</span> <span style="color:#0000e6; ">"http://testng.org/testng-1.0.dtd"</span> <span style="color:#004a43; ">&gt;</span>

<span style="color:#a65700; ">&lt;</span><span style="color:#5f5035; ">suite</span> <span style="color:#274796; ">name</span><span style="color:#808030; ">=</span><span style="color:#0000e6; ">"</span><span style="color:#0000e6; ">SuiteSimpleTests</span><span style="color:#0000e6; ">"</span>    <span style="color:#274796; ">verbose</span><span style="color:#808030; ">=</span><span style="color:#0000e6; ">"</span><span style="color:#0000e6; ">1</span><span style="color:#0000e6; ">"</span> <span style="color:#a65700; ">&gt;</span>
  <span style="color:#a65700; ">&lt;</span><span style="color:#5f5035; ">test</span> <span style="color:#274796; ">name</span><span style="color:#808030; ">=</span><span style="color:#0000e6; ">"</span><span style="color:#0000e6; ">GroupTest1</span><span style="color:#0000e6; ">"</span>   <span style="color:#a65700; ">&gt;</span>
    <span style="color:#a65700; ">&lt;</span><span style="color:#5f5035; ">classes</span><span style="color:#a65700; ">&gt;</span>
      <span style="color:#a65700; ">&lt;</span><span style="color:#5f5035; ">class</span> <span style="color:#274796; ">name</span><span style="color:#808030; ">=</span><span style="color:#0000e6; ">"</span><span style="color:#0000e6; ">test.SimpleTestNG</span><span style="color:#0000e6; ">"</span><span style="color:#a65700; ">/&gt;</span>
      <span style="color:#a65700; ">&lt;</span><span style="color:#5f5035; ">class</span> <span style="color:#274796; ">name</span><span style="color:#808030; ">=</span><span style="color:#0000e6; ">"</span><span style="color:#0000e6; ">test.SimpleTestNG2</span><span style="color:#0000e6; ">"</span><span style="color:#a65700; ">/&gt;</span>
    <span style="color:#a65700; ">&lt;/</span><span style="color:#5f5035; ">classes</span><span style="color:#a65700; ">&gt;</span>
  <span style="color:#a65700; ">&lt;/</span><span style="color:#5f5035; ">test</span><span style="color:#a65700; ">&gt;</span>
<span style="color:#a65700; ">&lt;/</span><span style="color:#5f5035; ">suite</span><span style="color:#a65700; ">&gt;</span></pre>
<p>Como vez, los frameworks son bastante simples de utilizar, es solo cuestión de que te hagas el hábito de trabajar pensando y orientandote al testing. Pronto verás los beneficios que te trae para la calidad de tu software.<br />
Espero te haya servido.</p>
<p>Enlaces: <a href="http://www.junit.org/">JUnit</a> <a href="http://testng.org/doc/documentation-main.html">TestNG</a></p>
]]></content:encoded>
			<wfw:commentRss>http://gustavopeiretti.com/2009/mejora-calidad-software-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

