blob: 9a6b0f1a02afda2bed3b38b8720f23e804968e27 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Apache TomEE</title>
<meta name="description"
content="Apache TomEE is a lightweight, yet powerful, JavaEE Application server with feature rich tooling." />
<meta name="keywords" content="tomee,asf,apache,javaee,jee,shade,embedded,test,junit,applicationcomposer,maven,arquillian" />
<meta name="author" content="Luka Cvetinovic for Codrops" />
<link rel="icon" href="../favicon.ico">
<link rel="icon" type="image/png" href="../favicon.png">
<meta name="msapplication-TileColor" content="#80287a">
<meta name="theme-color" content="#80287a">
<link rel="stylesheet" type="text/css" href="../css/normalize.css">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="../css/owl.css">
<link rel="stylesheet" type="text/css" href="../css/animate.css">
<link rel="stylesheet" type="text/css" href="../fonts/font-awesome-4.1.0/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="../fonts/eleganticons/et-icons.css">
<link rel="stylesheet" type="text/css" href="../css/jqtree.css">
<link rel="stylesheet" type="text/css" href="../css/idea.css">
<link rel="stylesheet" type="text/css" href="../css/cardio.css">
<script type="text/javascript">
<!-- Matomo -->
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
/* We explicitly disable cookie tracking to avoid privacy issues */
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = "//matomo.privacy.apache.org/";
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', '5']);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.async = true;
g.src = u + 'matomo.js';
s.parentNode.insertBefore(g, s);
})();
<!-- End Matomo Code -->
</script>
</head>
<body>
<div class="preloader">
<img src="../img/loader.gif" alt="Preloader image">
</div>
<nav class="navbar">
<div class="container">
<div class="row"> <div class="col-md-12">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/" title="Apache TomEE">
<span>
<img
src="../img/apache_tomee-logo.svg"
onerror="this.src='../img/apache_tomee-logo.jpg'"
height="50"
>
</span>
</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right main-nav">
<li><a href="../docs.html">Documentation</a></li>
<li><a href="../community/index.html">Community</a></li>
<li><a href="../security/security.html">Security</a></li>
<li><a class="btn btn-accent accent-orange no-shadow" href="../download.html">Downloads</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div></div>
</div>
<!-- /.container-fluid -->
</nav>
<div id="main-block" class="container main-block">
<div class="row title">
<div class="col-md-12">
<div class='page-header'>
<h1>Creating itests</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="sect1">
<h2 id="_openejb_itests">OpenEJB itests</h2>
<div class="sectionbody">
<div class="paragraph">
<p>The OpenEJB itests module is a framework to create EJB test cases that are designed according to the JUnit rules, i.e.
they all have setUp, tests and tearDown methods.
Since it&#8217;s JUnit-based, you can do whatever you could do in JUnit.</p>
</div>
<div class="paragraph">
<p>This page describes the steps to create EJB test cases.</p>
</div>
<div class="sect2">
<h3 id="_how_itests_work">How itests work</h3>
<div class="paragraph">
<p>The itests module lives in OpenEJB&#8217;s repository in the <em>modules\itests</em> directory.
Setting up the test environment to execute the itests is based on (<a href="http://svn.apache.org/repos/asf/maven/maven-1/plugins-sandbox/trunk/itest/">maven-itest-plugin-1.0 plugin</a> ).</p>
</div>
<div class="paragraph">
<p>Take a look at maven.xml in modules\itests directory.
There you&#8217;ll see that the default goal is <em>ejb:install</em>, which in turn executes <em>itest</em>.
When the EJBs (todo: describe it a bit more) are done, the <em>itest:setup</em> goal is executed, which starts the real game.
First, <em>org/openejb/Security</em> configuration is started.
Once it&#8217;s done, <em>openejb-itests-xxx.jar</em> is deployed, which is <em>org/openejb/Itests</em> configuration to be started, afterwards.
When the configurations are deployed and started, the maven-itest-plugin executes junit (see <a href="http://ant.apache.org/manual/OptionalTasks/junit.html">Ant JUnit task documentation</a> and project.properties of the itests module).
The project.properties file configures which itests are run and some other stuff.</p>
</div>
<div class="paragraph">
<p>The first itest test case is <em>org.openejb.test.entity.cmp.CmpTestSuite</em>.
Consult this for more information.
Then the others defined in <em>maven.itest.includes</em> property are executed.</p>
</div>
<div class="paragraph">
<p>The order in which the itests are executed is important, so the first order is set up via the maven.itest.includes property, then the test suites add their tests in some order, and finally the method names in the test classes put yet another order.
So, be careful what name your test method name will become.
It may influence the order.</p>
</div>
<div class="paragraph">
<p>Some EJBs access database resources.
It&#8217;s even more important for CMPs.
The itests module uses the database as defined in the <em>openejb.test.database</em> property.
It&#8217;s currently defined in the <em>project.properties</em> file of the module.
You can change its value to whatever you wish using the Maven property setting approaches (-D on the command line, project.properties, build.properties in your home directory or the project you work in).</p>
</div>
<div class="paragraph">
<p>So, the last important information is how the junit tests access the server resources - EJBs.
It&#8217;s done via executing session beans that in turn get at the test EJBs, mostly CMPs.
It&#8217;s also possible that the CMP itests will be accessed directly without having to pass on the call through a session bean.</p>
</div>
<div class="paragraph">
<p>If itests are part of a larger project structure you can disable executing it using the <em>maven.itest.skip</em> property.
Set it to <em>true</em> and Maven won&#8217;t run the itests.</p>
</div>
</div>
<div class="sect2">
<h3 id="_simple_cmp_2_1_itest">Simple CMP 2.1 itest</h3>
<div class="sect3">
<h4 id="_database_setup">Database setup</h4>
<div class="paragraph">
<p>The itests default database is Derby.
The class - <em>org.openejb.test.DerbyTestDatabase</em> - is instantiated upon executing <em>org.openejb.testTestManager.getDatabase()</em> in each test case&#8217;s <em>setUp()</em> method.
Remember, you can define any other database using the <em>openejb.test.database</em> property or do initialization of your own database choice in the setUp() method.</p>
</div>
<div class="paragraph">
<p>The current implementation of database initialization is based on two DerbyTestDatabse methods: <em>createCMP2Model()</em> and <em>dropCMP2Model()</em> that create and drop database structure, accordingly.</p>
</div>
</div>
<div class="sect3">
<h4 id="_cmp_2_1_deployment">CMP 2.1 deployment</h4>
<div class="paragraph">
<p>{info:title=Information} Unless specified, all directories are relative to <em>modules/itests</em> directory and commands are executed in it.
{info}</p>
</div>
<div class="paragraph">
<p>A Maven project can produce one build artefact.
It&#8217;s very important to keep in mind whenever your tests are to be based on a EJB that&#8217;s not built by default.
The default EJBs are defined in <em>modules/itests/src/ejb/META-INF/ejb-jar.xml</em>.
The corresponding deployment plan - the <em>openejb-jar.xml</em> file is in <em>modules/itests/src/ejb/META-INF/openejb-jar.xml</em>.</p>
</div>
<div class="paragraph">
<p>If you want to test your own EJB, you need to build it yourself, i.e.
describe the build and deployment in <em>modules/itests/maven.xml</em> in the pregoal of <em>itest:setup</em>.</p>
</div>
<div class="paragraph">
<p>In the following example, Ant&#8217;s jar builds openejb-cmp2-petstore.jar file, which in turn is distributed and started in Geronimo.
The <em>id</em> attribute of <em>deploy:start</em> is as specified in the module&#8217;s deployment plan.
See <a href="http://wiki.apache.org/geronimo/Deployment">Geronimo Deployment</a> for more information about Geronimo deployment plans.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>&lt;ant:jar destfile="${basedir}/target/openejb-cmp2-petstore.jar"&gt;
&lt;fileset dir="${basedir}/target/classes"&gt;
&lt;include name="**/cmp2/petstore/*.class"/&gt;
&lt;include name="**/TestFailureException.class"/&gt;
&lt;/fileset&gt;
&lt;metainf dir="${basedir}/src/cmp2/petstore" includes="*.xml"/&gt;
&lt;/ant:jar&gt;
&lt;deploy:distribute
uri="deployer:geronimo:jmx:rmi://localhost/jndi/rmi:/JMXConnector"
username="system"
password="manager"
module="${basedir}/target/openejb-cmp2-petstore.jar"
/&gt;
&lt;deploy:start
uri="deployer:geronimo:jmx:rmi://localhost/jndi/rmi:/JMXConnector"
username="system"
password="manager"
id="org/openejb/cmp2/petstore"/&gt;</pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="_execution">Execution</h4>
<div class="paragraph">
<p>When EJB classes, deployment descriptor and plan, maven.xml are all set up, it&#8217;s time to execute your tests.
In order to run itests you will run Maven in <em>modules/itests</em> directory.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>~/openejb/modules/itests
$ maven</pre>
</div>
</div>
<div class="paragraph">
<p>It&#8217;s also possible to override project properties and run only some test cases.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>~/openejb/modules/itests
$ maven -Dmaven.itest.includes=**/Cmp2TestSuite.java</pre>
</div>
</div>
<div class="paragraph">
<p>When a failure occurs, you should take a look at the result file of the failed test suite in <em>target/itest-reports</em>, e.g.</p>
</div>
<div class="literalblock">
<div class="content">
<pre>~/openejb/modules/itests
$ maven -Dmaven.itest.includes=**/Cmp2TestSuite.java -o
...
[junit] Tests run: 113, Failures: 1, Errors: 0, Time elapsed: 22,132 sec
[junit] [ERROR] TEST org.openejb.test.entity.cmp2.Cmp2TestSuite FAILED
...
BUILD FAILED
File...... C:\Documents and Settings\root\.maven\cache\maven-itest-plugin-1.0\plugin.jelly
Element... fail
Line...... 166
Column.... 64
There were test failures.
Total time: 2 minutes 3 seconds
Finished at: Sun Jul 17 17:48:36 CEST 2005
$ more target/itest-reports/TEST-org.openejb.test.entity.cmp2.Cmp2TestSuite.txt
Testsuite: org.openejb.test.entity.cmp2.Cmp2TestSuite
Tests run: 113, Failures: 1, Errors: 0, Time elapsed: 22,132 sec
Testcase: PetstoreTests.create: FAILED
Received Exception class java.lang.NullPointerException : null
junit.framework.AssertionFailedError: Received Exception class java.lang.NullPointerException : null
at org.openejb.test.entity.cmp2.PetstoreTests.test01_create(PetstoreTests.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.openejb.test.NumberedTestCase.runTestMethod(NumberedTestCase.java:195)
at org.openejb.test.NumberedTestCase$3.protect(NumberedTestCase.java:169)
at org.openejb.test.NumberedTestCase.run(NumberedTestCase.java:172)
at org.openejb.test.NumberedTestCase.run(NumberedTestCase.java:141)
at org.openejb.test.TestSuite.run(TestSuite.java:71)
...</pre>
</div>
</div>
<div class="paragraph">
<p>Complete execution log is in <em>target/openejb/var/log/openejb.log</em> of the itests module.</p>
</div>
<div class="sect4">
<h5 id="_running_the_tests_in_eclipse">Running the Tests in Eclipse.</h5>
<div class="paragraph">
<p>The steps for running the iTests inside of Eclipse are given below.
They are</p>
</div>
<div class="paragraph">
<p>1) For Local Interface Tests, the class to be run is <em>org.apache.openejb.iTest</em>.
2) For Remote Interface Tests, the class to be run is <em>org.apache.openejb.RemoteiTest</em>.</p>
</div>
<div class="paragraph">
<p>In both the cases you need to give <em>'-Dopenejb.home=target/test-classes/'</em> as a vm argument for the tests to run.</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="margin-bottom: 30px;"></div>
<footer>
<div class="container">
<div class="row">
<div class="col-sm-6 text-center-mobile">
<h3 class="white">Be simple. Be certified. Be Tomcat.</h3>
<h5 class="light regular light-white">"A good application in a good server"</h5>
<ul class="social-footer">
<li><a href="https://www.facebook.com/ApacheTomEE/"><i class="fa fa-facebook"></i></a></li>
<li><a href="https://twitter.com/apachetomee"><i class="fa fa-twitter"></i></a></li>
</ul>
<h5 class="light regular light-white">
<a href="../privacy-policy.html" class="white">Privacy Policy</a>
</h5>
</div>
<div class="col-sm-6 text-center-mobile">
<div class="row opening-hours">
<div class="col-sm-3 text-center-mobile">
<h5><a href="../latest/docs/" class="white">Documentation</a></h5>
<ul class="list-unstyled">
<li><a href="../latest/docs/admin/configuration/index.html" class="regular light-white">How to configure</a></li>
<li><a href="../latest/docs/admin/file-layout.html" class="regular light-white">Dir. Structure</a></li>
<li><a href="../latest/docs/developer/testing/index.html" class="regular light-white">Testing</a></li>
<li><a href="../latest/docs/admin/cluster/index.html" class="regular light-white">Clustering</a></li>
</ul>
</div>
<div class="col-sm-3 text-center-mobile">
<h5><a href="../latest/examples/" class="white">Examples</a></h5>
<ul class="list-unstyled">
<li><a href="../latest/examples/simple-cdi-interceptor.html" class="regular light-white">CDI Interceptor</a></li>
<li><a href="../latest/examples/rest-cdi.html" class="regular light-white">REST with CDI</a></li>
<li><a href="../latest/examples/ejb-examples.html" class="regular light-white">EJB</a></li>
<li><a href="../latest/examples/jsf-managedBean-and-ejb.html" class="regular light-white">JSF</a></li>
</ul>
</div>
<div class="col-sm-3 text-center-mobile">
<h5><a href="../community/index.html" class="white">Community</a></h5>
<ul class="list-unstyled">
<li><a href="../community/contributors.html" class="regular light-white">Contributors</a></li>
<li><a href="../community/social.html" class="regular light-white">Social</a></li>
<li><a href="../community/sources.html" class="regular light-white">Sources</a></li>
</ul>
</div>
<div class="col-sm-3 text-center-mobile">
<h5><a href="../security/index.html" class="white">Security</a></h5>
<ul class="list-unstyled">
<li><a href="https://apache.org/security" target="_blank" class="regular light-white">Apache Security</a></li>
<li><a href="https://apache.org/security/projects.html" target="_blank" class="regular light-white">Security Projects</a></li>
<li><a href="https://cve.mitre.org" target="_blank" class="regular light-white">CVE</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="row bottom-footer text-center-mobile">
<div class="col-sm-12 light-white">
<p>Copyright &copy; 1999-2022 The Apache Software Foundation, Licensed under the Apache License, Version 2.0. Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
</div>
</div>
</div>
</footer>
<!-- Holder for mobile navigation -->
<div class="mobile-nav">
<ul>
<li><a hef="../latest/docs/admin/index.html">Administrators</a>
<li><a hef="../latest/docs/developer/index.html">Developers</a>
<li><a hef="../latest/docs/advanced/index.html">Advanced</a>
<li><a hef="../community/index.html">Community</a>
</ul>
<a href="#" class="close-link"><i class="arrow_up"></i></a>
</div>
<!-- Scripts -->
<script src="../js/jquery-1.11.1.min.js"></script>
<script src="../js/owl.carousel.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/wow.min.js"></script>
<script src="../js/typewriter.js"></script>
<script src="../js/jquery.onepagenav.js"></script>
<script src="../js/tree.jquery.js"></script>
<script src="../js/highlight.pack.js"></script>
<script src="../js/main.js"></script>
</body>
</html>