<?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>simonpena.com &#187; JUnit</title>
	<atom:link href="http://simonpena.com/blog/tag/junit/feed/" rel="self" type="application/rss+xml" />
	<link>http://simonpena.com/blog</link>
	<description>Una mezcla heterogénea de tecnología y desvaríos</description>
	<lastBuildDate>Sun, 13 May 2012 12:37:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Misunderstanding Hibernate</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=misunderstanding-hibernate</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 18:30:40 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[CascadeType]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=45</guid>
		<description><![CDATA[After some more tests, I've found that I was wrong with some ideas I had about Hibernate.<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/' addthis:title='Misunderstanding Hibernate '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>These last days I've been doing more <strong>JUnit</strong> tests. And thanks to them, I've discovered some serious misunderstanding I had about <strong>Hibernate</strong>, <strong>Cascading</strong> (yes, again) and <strong>ManyToOne</strong>, <strong>OneToMany</strong> relationships.</p>
<p>The first mistake I have was start coding too fast. I started following a tutorial, but I confess that I focused too much on code, an little on the theory. Following that tutorial I set a <strong>OneToMany</strong> relationship between an <em>user</em> and his <em>accounts</em>. Somehow, I thought that the <em>account</em> list in the <em>user</em> side would get filled <a title="automagically - Wiktionary" href="http://en.wiktionary.org/wiki/automagically" target="_blank">automagically</a>, driven by the annotations I was using. As the accounts got dropped from the database when I deleted an user, I thought everything was alright, and I didn't think about too much.</p>
<p>Yesterday, doing some tests on the <em>category</em> entity, I found that in a child-parent relationship, the child didn't have its parent <em>category</em> updated when I deleted that parent <em>category</em>. In the database I had set <strong>"ON DELETE SET NULL"</strong>, and looking at the database while performing the tests showed that it was working there. But <strong>Hibernate</strong> didn't seem aware of that behavior, and the link to the old parent <em>category</em> still appeared in the child. That led me, after some tries, to understand how cascading was supposed to work and how badly I was using it. I had several cascading annotations in the <strong>ManyToOne</strong> side of some relationships, which meant that when I made a change on the <em>many</em> side, the other side was getting affected. And that wasn't the behavior I was expecting!</p>
<p>I went to the documentation to learn how should annotations work, and what did they mean according to the context they were used in. After that, I removed most of my annotations, and made some changes, in the <em>update</em> and <em>delete</em> methods in my <em>DAOs</em> to spread the changes when that was needed.</p>
<p>Now, an <em>user</em> has a list of <em>accounts</em>. When an <em>account</em> is created, it sets a reference to the <em>user</em> who has it. And makes that <em>user</em> add it to his <em>account</em> list. When an <em>account</em> is deleted, it makes the <em>user</em> remove it from the <em>account</em> list. As the <em>account</em> <em>operations</em> have a reference to an <em>account</em>, <strong>Hibernate</strong> is responsible to update that reference.</p>
<p>The <em>categories</em> behavior is similar. When a root <em>category</em> is created, it creates an empty list of child <em>categories</em>. When you add a child <em>category</em> to a parent <em>category</em>, you add that child to his parent list. When deleting the parent, you have to go through the list, updating all the references in his children. When deleting a child, you have to remove it from his parent list.</p>
<p>After these changes, everything is working now. I know that some of the tests I've done are too bound to my interpretation of the behavior they have to show, and so they may be wrong. But at least I get rid of part of the errors I still have.</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/' addthis:title='Misunderstanding Hibernate '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/misunderstanding-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing&#8230;</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/testing/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=testing</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/testing/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 05:12:12 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[id to load is required for loading]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=40</guid>
		<description><![CDATA[Some more JUnit test cases, and some more actions implemented. The application keeps growing.<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/testing/' addthis:title='Testing&#8230; '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Today I spent all the afternoon testing. It may seem a tedious task, but after a while it gets quite challenging: you know there are errors in your code, so the objective is finding them. And while doing those tests, correcting the actions which showed wrong, I implemented some new ones.</p>
<p>The application is getting closer and closer to an usable state, and I expect it to be running in about two or three weeks.</p>
<p>Today's odd error was a lot easier than the others: <strong>id to load is required for loading</strong>. Maybe not the most complete description, before you find the place it is failing, but good enough once you go to the correct line number. I was using some fields in an Action which I haven't initialized yet. D'oh!</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/testing/' addthis:title='Testing&#8230; '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/testing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mind the Cascade</title>
		<link>http://simonpena.com/blog/proyectos/pennybank/mind-the-cascade/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mind-the-cascade</link>
		<comments>http://simonpena.com/blog/proyectos/pennybank/mind-the-cascade/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 03:33:00 +0000</pubDate>
		<dc:creator>Simón</dc:creator>
				<category><![CDATA[Pennybank]]></category>
		<category><![CDATA[CascadeType]]></category>
		<category><![CDATA[deleted entity passed to persist]]></category>
		<category><![CDATA[detached entity passed to persist]]></category>
		<category><![CDATA[implementation]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://pennybank.wordpress.com/?p=28</guid>
		<description><![CDATA[Refactoring JUnit tests and changing the way account operations are organized has revealed some bad annotations.<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/mind-the-cascade/' addthis:title='Mind the Cascade '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Today I decided to go through the model layer, implementing as many <a title="JUnit.org" href="http://www.junit.org/" target="_blank">JUnit</a> tests as I could, and completing all the actions I hadn't implemented yet. As I said <a title="Classifying account operations" href="http://www.simonpena.com/blog/proyectos/pennybank/classifying-account-operations" target="_blank">before</a>, I had just decided to stick to <em>one-category-at-a-time </em>scheme, having a hierarchical relationship between categories.</p>
<p>I thought it would be easy: change the Category entity, check the <em>on delete </em>and <em>on update </em>constraints in the database creation script, refactor and go on.I also had in mind some refactor in my JUnit tests: I have just two cases, one for each Facade, and several methods inside, for each of the actions in that facade. But as the application was getting larger and larger, I decided to have one JUnit case for each Facade Method, with several test methods for each of the possible scenarios I can think of.</p>
<p>So I was there, refactoring tests, moving some code to the <a title="@BeforeClass annotation" href="http://junit.org/apidocs/org/junit/BeforeClass.html" target="_blank"><em>setUpBeforeClass</em></a> and <a title="@AfterClass annotation" href="http://junit.org/apidocs/org/junit/AfterClass.html" target="_blank"><em>tearDownAfterClass</em></a> methods, when everything started to break:</p>
<p><strong>deleted entity passed to persist</strong> and <strong>detached entity passed to persist</strong>. After some research in Google, I understood what could be causing the second error, so I solved it:</p>
<p>I was using an entity in a call to persist which had been "left" unchecked by the entity manager  (detached is the word, but I didn't know it). I got the entity again by invoking <em>find</em>, and passed it fresh, and it worked out.</p>
<p>But the first problem was harder. Google didn't shed any light on me (apparently), as everything was about deleted entities which were still referenced by others related to them. I didn't have any entity left: this was happening in the <em>tearDownAfterClass</em> method, and I was removing the entities left after the test. And then I realized that most of the clean up was supposed to be done by cascading. So, maybe <a title="CascadeType (JAVA EE 5)" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/CascadeType.html" target="_blank">cascading</a> tags were incorrect. I checked out my classes, and I changed some of them: putting the explicit ones instead of <strong>CascadeType.ALL</strong>. I still have to test some of the <em>Update</em> methods, but for today, it's fixed.</p>
<div class="addthis_toolbox addthis_default_style " addthis:url='http://simonpena.com/blog/proyectos/pennybank/mind-the-cascade/' addthis:title='Mind the Cascade '  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_button_google_plusone" g:plusone:size="medium"></a><a class="addthis_counter addthis_pill_style"></a></div>]]></content:encoded>
			<wfw:commentRss>http://simonpena.com/blog/proyectos/pennybank/mind-the-cascade/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

