<?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>lstierneyltd &#187; junit</title>
	<atom:link href="http://lstierneyltd.com/blog/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://lstierneyltd.com/blog</link>
	<description>Yet another development blog</description>
	<lastBuildDate>Wed, 13 Jul 2011 12:55:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Unit Test Private Java Methods using Reflection</title>
		<link>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/</link>
		<comments>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/#comments</comments>
		<pubDate>Thu, 05 May 2011 17:02:06 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[How to's]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=254</guid>
		<description><![CDATA[Introduction I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments. Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against! Example The class under test The (Reflection Based) [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>I realise that the thought of Unit Testing private methods in Java classes can be like a red rag to a bull for some people and I understand the arguments.</p>
<p>Therefore, I present the following as a &#8220;how to&#8221;, not a moral argument for or against!</p>
<h3>Example</h3>
<h4>The class under test</h4>
<pre class="brush: java; title: ; notranslate">
public class Product() {
    private String privateMethod(String id) {
    //Do something private
    return &quot;product_&quot; + id;
  }
}
</pre>
<h4>The (Reflection Based) Unit Test</h4>
<pre class="brush: java; title: ; notranslate">
import java.lang.reflect.Method;

import static org.junit.Assert.*;
import org.junit.Test;

public class ProductTest {
    private Product product; // the class under test
    private Method m;
    private static String METHOD_NAME = &quot;privateMethod&quot;;
    private Class[] parameterTypes;
    private Object[] parameters;

    @Before
    public void setUp() throws Exception {
        product = new Product();
        parameterTypes = new Class[1];
        parameterTypes[0] = java.lang.String.class;
        m = product.getClass().getDeclaredMethod(METHOD_NAME, parameterTypes);
        m.setAccessible(true);
        parameters = new Object[1];
    }

    @Test
    public void testPrivateMethod() throws Exception {
        parameters[0] = &quot;someIdentifier&quot;;
        String result = (String) m.invoke(product, parameters); 

        //Do your assertions
        assertNotNull(result);
    }
}
</pre>
<h3>Update</h3>
<p>I&#8217;ve since been told that if <a href="http://code.google.com/p/dp4j/" target="_blank">dp4j.jar</a> is in the classpath at compile-time, it will inject the necessary reflection to make this work. I haven&#8217;t had time to try this yet so YMMV.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/unit-test-private-java-methods-using-reflection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Validation in Annotation Based Validating Spring Beans</title>
		<link>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 08:57:07 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=245</guid>
		<description><![CDATA[Motivation I added validation, via annotations, to a Spring &#8220;Model&#8221; bean. I needed someway to Unit Test this validation, without running the container and without initialising the Spring context. The bean (simplified) The Unit Test More It actually took me a few hours to work the above test out (simple as it is). If I [...]]]></description>
			<content:encoded><![CDATA[<h3>Motivation</h3>
<p>I added validation, via annotations, to a Spring &#8220;Model&#8221; bean. I needed someway to Unit Test this validation, <em>without</em> running the container and <em>without</em> initialising the Spring context.</p>
<h3>The bean (simplified)</h3>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

public final class ProductModel {

    @NotNull
    @Size(max=100)
    @Pattern(regexp=&quot;[^\n^\t^\r]+&quot;, message=&quot;Long Name must not contain New Lines, Carriage Returns or Tabs&quot;)
    private String longName;

    @Size(max=20)
    private String shortName;

    // rest snipped for brevity
}
</pre>
<h3>The Unit Test</h3>
<pre class="brush: java; title: ; notranslate">
package foo.bar;

import java.util.Set;

import javax.validation.ConstraintViolation;

import junit.framework.Assert;

import org.hibernate.validator.HibernateValidator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class Temp {
    private LocalValidatorFactoryBean localValidatorFactory;

    @Before
    public void setup() {
        localValidatorFactory = new LocalValidatorFactoryBean();
        localValidatorFactory.setProviderClass(HibernateValidator.class);
        localValidatorFactory.afterPropertiesSet();
    }
    @Test
    public void testLongNameWithInvalidCharCausesValidationError() {
        final ProductModel productModel = new ProductModel();
        productModel.setLongName(&quot;A long name with\t a Tab character&quot;);
    	Set&lt;ConstraintViolation&lt;ProductModel&gt;&gt; constraintViolations = localValidatorFactory.validate(productModel);
    	Assert.assertTrue(&quot;Expected validation error not found&quot;, constraintViolations.size() == 1);
    }
}
</pre>
<h3>More</h3>
<p>It actually took me a few hours to work the above test out (simple as it is). If I hadn&#8217;t stumbled upon <a href="https://src.springsource.org/svn/spring-framework/trunk/org.springframework.context/src/test/java/org/springframework/validation/beanvalidation/ValidatorFactoryTests.java">these Spring Unit Tests</a>, I might never have got it.</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/spring-examples/unit-testing-validation-in-annotation-based-validating-spring-beans/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JUnit 4 Test Class with annotations</title>
		<link>http://lstierneyltd.com/blog/development/examples/junit-4-test-class-with-annotations/</link>
		<comments>http://lstierneyltd.com/blog/development/examples/junit-4-test-class-with-annotations/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 11:15:05 +0000</pubDate>
		<dc:creator>lawrence</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[junit]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://lstierneyltd.com/blog/?p=139</guid>
		<description><![CDATA[A simple example of a JUnit 4 Test class marked up with annotations. Points of note The static import of org.junit.Assert.*]]></description>
			<content:encoded><![CDATA[<p>A simple example of a JUnit 4 Test class marked up with annotations.<br />
<code><span id="more-139"></span></code></p>
<pre class="brush: plain; title: ; notranslate">
import org.junit.*;
import static org.junit.Assert.*; 

public class SampleTest {
    private java.util.List emptyList; 

    /**
     * Sets up the test fixture.
     * (Called before every test case method.)
     */
    @Before
    public void setUp() {
        emptyList = new java.util.ArrayList();
    } 

    /**
     * Tears down the test fixture.
     * (Called after every test case method.)
     */
    @After
    public void tearDown() {
        emptyList = null;
    } 

    @Test
    public void testSomeBehavior() {
        assertEquals(&quot;Empty list should have 0 elements&quot;, 0, emptyList.size());
    } 

    @Test(expected=IndexOutOfBoundsException.class)
    public void testForException() {
        Object o = emptyList.get(0);
    }
}
</pre>
<h4>Points of note</h4>
<p>The <em>static</em> import of org.junit.Assert.*</p>
]]></content:encoded>
			<wfw:commentRss>http://lstierneyltd.com/blog/development/examples/junit-4-test-class-with-annotations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

