Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring/Hibernate/Junit example of testing DAO against HSQLDB
    text
    copied!<p>I'm working on trying to implement a <em>JUnit</em> test to check the functionality of a DAO. (The DAO will create/read a basic object/table relationship).</p> <p>The trouble I'm having is the persistence of the DAO (for the non-test code) is being completed through an in-house solution using <em>Spring/Hibernate</em>, which eliminates the usual <code>*.hbm.xml</code>templates that most examples I have found contain.</p> <p>Because of this, I'm having some trouble understanding how to setup a <em>JUnit</em> test to implement the DAO to create/read (just very basic functionality) to an in-memory <em>HSQLDB</em>. I have found a few examples, but the usage of the in-house persistence means I can't extend some of the classes the examples show (I can't seem to get the application-context.xml setup properly).</p> <p>Can anyone suggest any projects/examples I could take a look at (or any documentation) to further my understanding of the best way to implement this test functionality? I feel like this should be really simple, but I keep running into problems implementing the examples I have found.</p> <p><strong>edit:</strong></p> <p>Here's my solution for better readability, for anyone who needs a hand getting things going:</p> <ul> <li><p>My <code>TestClass</code>:</p> <pre><code>@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContextTest-Example.xml") @Transactional public class ExampleDaoTest extends AbstractTransactionalJUnit4SpringContextTests { @Resource(name = "sessionFactory") private SessionFactory exampleSessionFactory; @Resource(name = "exampleDao") private ExampleDao exampleDao; </code></pre></li> <li><p>My <code>applicationContext.xml</code> file:</p> <pre><code>&lt;!-- List of Daos to be tested --&gt; &lt;bean id="exampleDao" class="org.myExample.ExampleDao"/&gt; &lt;!-- Datasource --&gt; &lt;bean id="example_dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt; &lt;property name="driverClassName" value="org.hsqldb.jdbcDriver"/&gt; &lt;property name="url" value="jdbc:hsqldb:mem:ExampleTest"/&gt; &lt;property name="username" value="sa"/&gt; &lt;property name="password" value=""/&gt; &lt;/bean&gt; &lt;!-- Session Factory --&gt; &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt; &lt;property name="dataSource" ref="example_dataSource"/&gt; &lt;property name="annotatedClasses"&gt; &lt;list&gt; &lt;value&gt;org.myExample.ExampleClass&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="hibernateProperties"&gt; .... left to user to choose properties &lt;/property&gt; &lt;/bean&gt; </code></pre></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload