<?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>Spock &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/spock/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:53:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Spock &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Guava: Testing equals(..) and hashcode(..)</title>
		<link>https://myshittycode.com/2015/04/28/guava-testing-equals-and-hashcode/</link>
					<comments>https://myshittycode.com/2015/04/28/guava-testing-equals-and-hashcode/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 28 Apr 2015 15:45:29 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Guava]]></category>
		<category><![CDATA[Spock]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=698</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we want to test the following equals(..):- A correctly implemented equals(..) must be reflexive, symmetric, transitive, consistent and handles null comparison. In another word, you have to write test cases to pass at least these 5 rules. Anything less is pure bullshit. SOLUTION You can write these tests yourself&#8230; or you can [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/04/28/guava-testing-equals-and-hashcode/">Guava: Testing equals(..) and hashcode(..)</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we want to test the following <b>equals(..)</b>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [5,6,7,8,9,10,11,12,13]; title: ; notranslate">
public class Person {
    private String name;
    private int age;

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Person person = (Person) o;
        return Objects.equal(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
</pre></div>


<p>A correctly implemented <b>equals(..)</b> must be reflexive, symmetric, transitive, consistent and handles null comparison.</p>



<p>In another word, you have to write test cases to pass at least these 5 rules. Anything less is pure bullshit.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>You can write these tests yourself&#8230; or you can leverage Guava&#8217;s EqualsTester. This library will test these 5 rules and ensure the generated hashcode matches too.</p>



<p>First, include the needed dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;com.google.guava&lt;/groupid&gt;
    &lt;artifactid&gt;guava-testlib&lt;/artifactid&gt;
    &lt;version&gt;18.0&lt;/version&gt;
    &lt;scope&gt;test&lt;/scope&gt;
&lt;/dependency&gt;
</pre></div>


<p>Instead of writing JUnit tests, I&#8217;ll be writing Spock specs, which is essentially built on top of Groovy, because it allows me to write very clear and clean tests.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
class PersonSpec extends Specification {

    def person = new Person(name: &#039;Mike&#039;, age: 10)

    def &quot;equals - equal&quot;() {
        when:
        new EqualsTester().
                addEqualityGroup(person,
                                 new Person(name: &#039;Mike&#039;, age: 10),
                                 new Person(name: &#039;Mike&#039;, age: 20)).
                testEquals()

        then:
        notThrown(AssertionFailedError.class)
    }

    def &quot;equals - not equal&quot;() {
        when:
        new EqualsTester().
                addEqualityGroup(person,
                                 new Person(name: &#039;Kurt&#039;, age: 10)).
                testEquals()

        then:
        thrown(AssertionFailedError.class)
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/04/28/guava-testing-equals-and-hashcode/">Guava: Testing equals(..) and hashcode(..)</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/04/28/guava-testing-equals-and-hashcode/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">698</post-id>	</item>
		<item>
		<title>Spock: Reading Test Data from CSV File</title>
		<link>https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/</link>
					<comments>https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 08 Aug 2014 18:02:45 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Spock]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=556</guid>

					<description><![CDATA[<p>Following up on my recent post about creating a Spock specification to read the test data from a CSV file without loading all the data into the memory, I created a CSVReader that implements Iterable that allows me to pull this off. You may download the source code here. With this implementation, I can now [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/">Spock: Reading Test Data from CSV File</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Following up on <a href="http://stackoverflow.com/questions/25189342/spock-reading-test-data-from-csv-file" target="_blank" rel="noopener">my recent post</a> about creating a Spock specification to read the test data from a CSV file without loading all the data into the memory, I created a <b>CSVReader</b> that implements <b>Iterable</b> that allows me to pull this off. You may <a href="https://github.com/choonchernlim/util/blob/master/src/main/java/com/choonchernlim/util/reader/CSVReader.java" target="_blank" rel="noopener">download the source code here</a>.</p>



<p>With this implementation, I can now write an elegant Spock specification:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
class MySpockSpec extends Specification {
    @Unroll
    def &quot;#firstNum + 1 == #secondNum&quot;() {
        expect:
        Integer.valueOf(firstNum as String) + 1 == Integer.valueOf(secondNum as String)

        where:
        &#x5B;firstNum, secondNum] &lt;&lt; new CSVReader(getClass().getClassLoader().getResourceAsStream(&quot;test.csv&quot;))
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/">Spock: Reading Test Data from CSV File</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/08/08/spock-reading-test-data-from-csv-file/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">556</post-id>	</item>
		<item>
		<title>Maven: Unable to Execute Spock Specs</title>
		<link>https://myshittycode.com/2014/07/21/maven-unable-to-execute-spock-specs/</link>
					<comments>https://myshittycode.com/2014/07/21/maven-unable-to-execute-spock-specs/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 21 Jul 2014 16:04:03 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Maven Surefire Plugin]]></category>
		<category><![CDATA[Spock]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=520</guid>

					<description><![CDATA[<p>PROBLEM When running mvn clean test, Maven Surefire Plugin doesn&#8217;t pick up *Spec.groovy test files. SOLUTION By default, Maven Surefire Plugin is configured to execute test files with the following patterns: **/Test*.java, **/*Test.java and **/*TestCase.java. To fix this, we need to modify the inclusion list for this plugin. Since both Java and Groovy files get [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/07/21/maven-unable-to-execute-spock-specs/">Maven: Unable to Execute Spock Specs</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>When running <b>mvn clean test</b>, Maven Surefire Plugin doesn&#8217;t pick up <b>*Spec.groovy</b> test files.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>By default, Maven Surefire Plugin is configured to execute test files with the following patterns: <b>**/Test*.java</b>, <b>**/*Test.java</b> and <b>**/*TestCase.java</b>.</p>



<p>To fix this, we need to modify the inclusion list for this plugin. Since both Java and Groovy files get compiled down to <b>*.class</b>, it is probably easier to just include <b>*.class</b> instead of <b>*.java</b> or <b>*.groovy</b>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;plugin&gt;
    &lt;groupid&gt;org.apache.maven.plugins&lt;/groupid&gt;
    &lt;artifactid&gt;maven-surefire-plugin&lt;/artifactid&gt;
    &lt;version&gt;2.17&lt;/version&gt;
    &lt;configuration&gt;
        &lt;includes&gt;
            &lt;include&gt;**/Test*.class&lt;/include&gt;
            &lt;include&gt;**/*Test.class&lt;/include&gt;
            &lt;include&gt;**/*TestCase.class&lt;/include&gt;
            &lt;include&gt;**/*Spec.class&lt;/include&gt;
        &lt;/includes&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/07/21/maven-unable-to-execute-spock-specs/">Maven: Unable to Execute Spock Specs</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/07/21/maven-unable-to-execute-spock-specs/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">520</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-21 00:51:32 by W3 Total Cache
-->