blob: 22eeaf3656d26cf5c02d7cb4a3a40bf86f27ff82 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>6.2&nbsp;Single bundle integration testing</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"><link rel="home" href="index.html" title="Creating an application with EclipseRT Virgo Web Server"><link rel="up" href="ch06.html" title="6.&nbsp;Testing GreenPages"><link rel="prev" href="ch06.html" title="6.&nbsp;Testing GreenPages"><link rel="next" href="ch06s03.html" title="6.3&nbsp;Contributing OSGi sourced dependencies"><!--Begin Google Analytics code--><script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
var pageTracker = _gat._getTracker("UA-2728886-3");
pageTracker._setDomainName("none");
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
</script><!--End Google Analytics code--></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">6.2&nbsp;Single bundle integration testing</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06.html">Prev</a>&nbsp;</td><th width="60%" align="center">6.&nbsp;Testing GreenPages</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch06s03.html">Next</a></td></tr></table><hr></div><div class="section" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="testing.greenpages.single.bundle"></a>6.2&nbsp;Single bundle integration testing</h2></div></div></div><p>
One of the most common forms of integration testing is ensuring that the object relational mapping in an
application is working properly. This kind of testing typically uses a data access object to retrieve data
from a live database. In this step a test case for the <code class="classname">JpaDirectory</code>
class is created.
</p><p>
Before proceeding, stop any Web Server instance that was previously running.
</p><p>
Open the <code class="classname">greenpages.jpa.JpaDirectorySpringContextTests</code> class in the
<code class="filename">src/test/java</code> source folder of the <code class="literal">greenpages.jpa</code> project. This
class contains a method that uses <span class="emphasis"><em>JUnit</em></span> to test that a search completes
correctly. Rather than instantiate
this class directly in the test, the Spring Test Framework is used to instantiate and inject a
<code class="classname">JpaDirectory</code> based on the <code class="literal">META-INF/spring/module-context.xml</code> file.
</p><p>
Add Spring Test Framework declarations to the test class. These declarations run the test with the
<code class="classname">SpringJunit4ClassRunner</code> and configure the test with the
<code class="literal">classpath:/META-INF/spring/module-context.xml</code> file:
</p><pre class="programlisting">@RunWith(SpringJUnit4ClassRunner.<span class="hl-keyword">class</span>)
@ContextConfiguration(locations = <span class="hl-string">"classpath:/META-INF/spring/module-context.xml"</span>)
@TestExecutionListeners(value = DependencyInjectionTestExecutionListener.<span class="hl-keyword">class</span>)
<span class="hl-keyword">public</span> <span class="hl-keyword">class</span> JpaDirectorySpringContextTests {
&#8230;
</pre><p>
Use Eclipse to suggest the necessary imports until there are no errors.
</p><p>
When this configuration is complete, click on the <span class="emphasis"><em>Run</em></span> drop-down menu and select
<span class="emphasis"><em>Run Configurations&#8230;</em></span>. In the the dialog that opens select
<span class="guimenu">JUnit</span> &#8594; <span class="guimenuitem">JpaDirectorySpringContextTests</span>
and press <span class="emphasis"><em>Run</em></span>.
</p><div class="mediaobject" align="center"><img src="images/testing-greenpages/jpa-test-runner.png" align="middle"></div><p>
</p><p>
This test run will fail because there is no <code class="interfacename">DataSource</code> bean to be injected;
it is typically sourced from the OSGi service registry at runtime:
</p><pre class="programlisting">Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'dataSource' is defined
</pre><p>
The next step will correct this error.
</p></div><!--Begin LoopFuse code--><script src="http://loopfuse.net/webrecorder/js/listen.js" type="text/javascript"></script><script type="text/javascript">
_lf_cid = "LF_48be82fa";
_lf_remora();
</script><!--End LoopFuse code--><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch06s03.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.&nbsp;Testing GreenPages&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;6.3&nbsp;Contributing OSGi sourced dependencies</td></tr></table></div></body></html>