blob: 065859650d0cc6bfa0b9e659b0c111d0d8dbdb9f [file] [log] [blame]
<!doctype html>
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Testing a Maven Enterprise Application</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Testing a Maven Enterprise Application - Apache NetBeans">
<meta name="author" content="Apache NetBeans">
<meta name="description" content="Testing a Maven Enterprise Application - Apache NetBeans">
<meta name="keywords" content="Apache NetBeans, Tutorials, Testing a Maven Enterprise Application">
<meta name="generator" content="Apache NetBeans">
<link rel="stylesheet" href="../../../../../../_/css/font-awesome.min.css">
<link rel="alternate" type="application/atom+xml" title="Apache NetBeans Blog" href="https://netbeans.apache.org/blogs/atom" />
<link rel="stylesheet" href="../../../../../../_/css/highlightjs/default.min.css">
<link rel="stylesheet" href="../../../../../../_/css/netbeans.css">
<link rel="apple-touch-icon" sizes="180x180" href="../../../../../../_/images/fav/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="../../../../../../_/images/fav/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="../../../../../../_/images/fav/favicon-16x16.png">
<link rel="manifest" href="../../../../../../_/images/fav/site.webmanifest">
<link rel="mask-icon" href="../../../../../../_/images/fav/safari-pinned-tab.svg" color="#5bbad5">
<meta name="msapplication-TileColor" content="#ffc40d">
<meta name="theme-color" content="#ffffff">
<link href="../../../../../../_/css/font-open-sans.css" rel="stylesheet">
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
</head>
<body>
<div class="title-bar" data-responsive-toggle="responsive-menu" data-hide-for="medium">
<button type="button" data-toggle="responsive-menu"><i style='font-size: 32px; color: #fff; padding: 8px' class='fa fa-bars'></i></button>
<div class="title-bar-title">Apache NetBeans</div>
</div>
<div class="top-bar" id="responsive-menu">
<div class='top-bar-left'>
<a class='title' href="../../../../../../index.html"><img src='../../../../../../_/images/apache-netbeans.svg' style='padding: 8px; height: 48px;'> Apache NetBeans</a>
</div>
<div class="top-bar-right">
<ul class="vertical medium-horizontal menu" data-responsive-menu="drilldown medium-dropdown">
<li> <input id="search-input" type="text" placeholder="Search the docs"> </li>
<li> <a href="../../../../../../front/main/community">Community</a> </li>
<li> <a href="../../../../../../front/main/participate">Participate</a> </li>
<li> <a href="../../../../../../front/main/blogs">Blog</a></li>
<li> <a href="../../../../../../front/main/help">Get Help</a> </li>
<li> <a href="https://plugins.netbeans.apache.org/">Plugins</a> </li>
<li> <a href="../../../../../../front/main/download">Download</a> </li>
</ul>
</div>
</div>
<!-- src/templates/news -->
<section class="hero news alternate">
<div class='grid-container'>
<div class='cell'>
<div class="annotation">Latest release</div>
<h1>Apache NetBeans 28</h1>
<p><a class="button success" href="../../../../../../front/main/download/nb28">Download</a></p>
</div>
</div>
</section>
<div class='grid-container main-content tutorial'>
<article class="doc">
<h1 class="sect0">Testing a Maven Enterprise Application</h1>
<div class="sectionbody">
<div class="admonitionblock note">
<table>
<tbody><tr>
<td class="icon"><i class="fa icon-note" title="Note"></i></td>
<td class="content">This tutorial needs a review.
You can <a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/maven-entapp-testing.adoc" title="Edit this tutorial in github">edit it in GitHub </a>
following these <a href="../../../../../../tutorial/main/kb/docs/contributing">contribution guidelines.</a></td>
</tr></tbody>
</table>
</div>
</div>
<div id="toc" class="toc">
<div id="toctitle"></div>
<ul class="sectlevel1">
<li><a href="#_using_maven_in_the_ide">Using Maven in the IDE</a></li>
<li><a href="#_creating_the_enterprise_application">Creating the Enterprise Application</a>
<ul class="sectlevel2">
<li><a href="#_creating_the_web_project">Creating the Web Project</a></li>
<li><a href="#_creating_the_entity_class">Creating the Entity Class</a></li>
<li><a href="#_creating_the_session_bean">Creating the Session Bean</a></li>
</ul>
</li>
<li><a href="#_creating_the_session_bean_test">Creating the Session Bean Test</a></li>
<li><a href="#_see_also">See Also</a></li>
</ul>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="paragraph">
<p>This tutorial demonstrates how to test a simple enterprise application using NetBeans IDE and Maven archetypes. In this tutorial you create an enterprise application that contains an entity class and a session bean. You will use a wizard to create a simple test class for the bean class and then run the test in the IDE. The test class will create an instance of the GlassFish embedded EJB container to test the connection to the database.</p>
</div>
<div class="paragraph">
<p><strong>Tutorial Exercises</strong></p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="#intro">Using Maven in the IDE</a></p>
</li>
<li>
<p><a href="#Exercise_1">Creating the Enterprise Application</a></p>
</li>
<li>
<p><a href="#Exercise_1a">Creating the Web Project</a></p>
</li>
<li>
<p><a href="#Exercise_1b">Creating the Entity Class</a></p>
</li>
<li>
<p><a href="#Exercise_1c">Creating the Session Bean</a></p>
</li>
<li>
<p><a href="#Exercise_2">Creating the Session Bean Test</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p><strong>To follow this tutorial, you need the following software and resources.</strong></p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 50%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">Software or Resource</th>
<th class="tableblock halign-left valign-top">Version Required</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="../../../../../../front/main/download/" class="xref page">NetBeans IDE</a></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">7.4, 8.0, Java EE bundle</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java Development Kit (JDK)</a></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">version 7 or 8</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">GlassFish Server Open Source Edition</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">4.0</p></td>
</tr>
</tbody>
</table>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The installer for GlassFish Server is included when you download the Java EE bundle. You can install and register GlassFish as part of the NetBeans IDE installation process.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><strong>Prerequisites</strong></p>
</div>
<div class="paragraph">
<p>This document assumes you have some basic knowledge of, or programming experience with, the following technologies:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>Java Programming</p>
</li>
<li>
<p>NetBeans IDE</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>Before starting this tutorial you may want to familiarize yourself with the following documentation.</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="../../../../../../wiki/main/wiki/MavenBestPractices/" class="xref page">Best Practices for Apache Maven in NetBeans IDE</a></p>
</li>
<li>
<p><a href="http://books.sonatype.com/mvnref-book/reference/introduction.html">Chapter 1. Introducing Apache Maven</a> (from <a href="http://books.sonatype.com/mvnref-book/reference/index.html">+Maven: The Complete Reference +</a>)</p>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_using_maven_in_the_ide"><a class="anchor" href="#_using_maven_in_the_ide"></a>Using Maven in the IDE</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Support for Maven is fully integrated in NetBeans IDE. Developing a project that uses the Maven framework is nearly identical to developing a project in Ant. However, there are some differences that are related to the way Maven builds projects and works with dependencies. The following tips can help you avoid some problems if you are creating a Maven application for the first time.</p>
</div>
<div class="paragraph">
<p><strong>Check your Maven settings</strong></p>
</div>
<div class="paragraph">
<p>If this is your first Maven project you will want to check the Maven configuration settings in the Options window. To complete this tutorial you must have Maven installed on your local system. Maven is bundled with the IDE and installed when you install the IDE.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Open the Options window in the IDE (Tools &gt; Options; NetBeans &gt; Preferences on Mac).</p>
</li>
<li>
<p>Select the Java category in the Options window and click the Maven tab.</p>
</li>
<li>
<p>Confirm that a Maven Home is specified.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>You can use the Maven version bundled with the IDE or specify the location of a local Maven installation (requires 2.0.9 or newer).</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click OK to close the Options window.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Maven support is automatically enabled when Java is enabled in the IDE. You will need to enable the Java EE plugin if it is not enabled.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p><strong>Update your Maven repositories</strong></p>
</div>
<div class="paragraph">
<p>Local and remote Maven repositories are used for code completion and when building projects. You should update the indexes for the Maven remote repositories to ensure that any artifacts you may need are readily available when you develop your project. You can configure how often the IDE checks for updates in the Maven tab of the Options window. You can perform an immediate check for updates and explore your local and remote Maven repositories in the Services window.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose Window &gt; Services to open the Services window.</p>
</li>
<li>
<p>Expand the Maven Repositories node in the Services window.</p>
</li>
<li>
<p>Right-click the repository node and choose Update Index in the popup menu.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Update Indexes, the IDE will check and download the most recent index for each of your Maven remote repositories. An index represents the current state of the artifacts located in the repository and is used to provide a reference to the artifacts that are available to your application. By default, the IDE does not download an artifact from a repository until the artifact is explicitly required.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Indexes are quite large and it can take some time to update them all.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>For more details about using Maven in NetBeans IDE, see the section on <a href="../../java/maven-hib-java-se/#02" class="xref page">Configuring Maven</a> in the <a href="../../java/maven-hib-java-se/" class="xref page">Creating a Maven Swing Application Using Hibernate</a> tutorial and also <a href="../../../../../../wiki/main/wiki/MavenBestPractices/" class="xref page">Best Practices for Apache Maven in NetBeans IDE</a>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_creating_the_enterprise_application"><a class="anchor" href="#_creating_the_enterprise_application"></a>Creating the Enterprise Application</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In this section you create a simple web application that contains an entity class and a session bean that accesses the entity class.</p>
</div>
<div class="sect2">
<h3 id="_creating_the_web_project"><a class="anchor" href="#_creating_the_web_project"></a>Creating the Web Project</h3>
<div class="paragraph">
<p>In this exercise you use the New Project wizard to create a Java EE web application from a Maven archetype. When you create the project with the wizard you will specify GlassFish Server as the target server.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Choose File &gt; New Project (Ctrl-Shift-N; ⌘-Shift-N on Mac) from the main menu to open the New Project wizard.</p>
</li>
<li>
<p>Select Web Application in the Maven category. Click Next.</p>
</li>
<li>
<p>Name the project <strong>mavenwebtestapp</strong> and set the project location. Click Next.</p>
</li>
<li>
<p>Select the GlassFish Server in the Server dropdown list.</p>
</li>
<li>
<p>Select Java EE 6 Web or Java EE 7 Web in the Java EE Version dropdown list. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish the IDE creates the web application and opens the project in the Projects window.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-testing-projects.png" alt="maven testing projects">
</div>
<div class="title">Figure 1. Projects window showing generated projects</div>
</div>
<div class="paragraph">
<p>If you expand the project node in the Projects window you can see that the <code>javaee-web-api</code> JAR is listed as a project dependency and that the JDK is listed as a Java dependency. The IDE generated the project POM ( <code>pom.xml</code> ) and the file is listed under the Project Files node.</p>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_entity_class"><a class="anchor" href="#_creating_the_entity_class"></a>Creating the Entity Class</h3>
<div class="paragraph">
<p>In this exercise you use the New File wizard to create an entity class. When you create the entity class you will select the <code>jdbc/sample</code> datasource in the wizard. You do not need to create or register a new datasource because the <code>jdbc/sample</code> datasource was registered when you installed the server.</p>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
If you want to create a new datasource or use a different datasource, the datasource must be registered on the server before you test the application using the embedded container. When testing the application using the embedded container the IDE will not register the datasource for you as it does when deploying to a GlassFish server instance.
</td>
</tr>
</table>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the project node and choose New &gt; Entity Class.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Alternatively, you can choose File &gt; New File (Ctrl-N; ⌘-N on Mac) from the main menu and select Entity Class in the Persistence category.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Type <strong>MyEntity</strong> for the Class Name.</p>
</li>
<li>
<p>Select <code>com.mycompany.mavenwebtestapp</code> as the Package and set the Primary Key Type to * <code>int</code> *.</p>
</li>
<li>
<p>Confirm that Create Persistence Unit is selected. Click Next.</p>
</li>
<li>
<p>Select <strong>jdbc/sample</strong> in the Data Source dropdown list.</p>
</li>
<li>
<p>Confirm that Use Java Transaction APIs is selected and select Drop and Create as the Table Generation Strategy. Click Finish.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-testing-pu.png" alt="maven testing pu">
</div>
<div class="title">Figure 2. Projects window showing generated projects</div>
</div>
<div class="paragraph">
<p>When you click Finish the IDE generates the MyEntity class and opens the class in the source editor. The IDE adds the <code>eclipselink</code> , <code>javax.persistence</code> and <code>org.eclipse.persistence.jpa.modelgen.processor</code> artifacts as project dependencies.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the source editor, add the private field <code>name</code> to the class.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">private String name;</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click in the editor and choose Getter and Setter in the Insert Code popup menu (Alt-Insert; Ctrl-I on Mac) to generate a getter and setter for the <code>name</code> field.</p>
</li>
<li>
<p>Add the following constructor.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">public MyEntity(int id) {
this.id = id;
name = "Entity number " + id + " created at " + new Date();
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following <code>@NamedQueries</code> and <code>@NamedQuery</code> annotations (in bold) to create a named SQL query that will find all records in the MyEntity table.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Entity
*@NamedQueries({
@NamedQuery(name = "MyEntity.findAll", query = "select e from MyEntity e")})*
public class MyEntity implements Serializable {</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Click the hint in the left margin next to the class declaration and choose the <strong>Create default constructor</strong> hint.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-testing-createconstructor.png" alt="maven testing createconstructor">
</div>
<div class="title">Figure 3. Projects window showing generated projects</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix the import statements (Ctrl-Shift-I; ⌘-Shift-I on Mac) to add import statements for <code>javax.persistence.NamedQuery</code> , <code>javax.persistence.NamedQueries</code> and <code>java.util.Date</code> . Save your changes.</p>
</li>
</ol>
</div>
</div>
<div class="sect2">
<h3 id="_creating_the_session_bean"><a class="anchor" href="#_creating_the_session_bean"></a>Creating the Session Bean</h3>
<div class="paragraph">
<p>In this exercise you will use a wizard to create a session facade for the <code>MyEntity</code> entity class. When you use the wizard to generate the facade the IDE will also generate an abstract facade that contains some methods such as <code>create</code> and <code>find</code> that are commonly used when accessing entity classes. You will then add two methods to the facade.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the project node and choose New &gt; Other.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Alternatively, you can choose File &gt; New File (Ctrl-N; ⌘-N on Mac) from the main menu to open the New File wizard.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Select Session Beans For Entity Classes in the Enterprise JavaBeans category. Click Next.</p>
</li>
<li>
<p>Select the <code>MyEntity</code> class from the list of Available Entity Classes and click Add. Click Next.</p>
</li>
<li>
<p>Use the default properties in the Generated Session Beans panel of the wizard. Click Finish.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you click Finish the IDE generates <code>AbstractFacade.java</code> and <code>MyEntityFacade.java</code> in the <code>com.mycompany.mavenwebtestapp</code> package and opens the classes in the source editor.</p>
</div>
<div class="paragraph">
<p>In the source editor you can see that the IDE generated code for <code>EntityManager</code> and added the <code>@PersistenceContext</code> annotation to specify the persistence unit.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Stateless
public class MyEntityFacade extends AbstractFacade&lt;MyEntity&gt; {
@PersistenceContext(unitName = "com.mycompany_mavenwebtestapp_war_1.0-SNAPSHOTPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public MyEntityFacade() {
super(MyEntity.class);
}
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Add the following methods to <code>MyEntityFacade.java</code> .</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> @PermitAll
public int verify() {
String result = null;
Query q = em.createNamedQuery("MyEntity.findAll");
Collection entities = q.getResultList();
int s = entities.size();
for (Object o : entities) {
MyEntity se = (MyEntity) o;
System.out.println("Found: " + se.getName());
}
return s;
}
@PermitAll
public void insert(int num) {
for (int i = 1; i &lt;= num; i++) {
System.out.println("Inserting # " + i);
MyEntity e = new MyEntity(i);
em.persist(e);
}
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix your imports to add the required import statements. Save your changes.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-testing-fiximports.png" alt="maven testing fiximports">
</div>
<div class="title">Figure 4. Projects window showing generated projects</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
Confirm that * <code>javax.persistence.Query</code> * is selected in the Fix All Imports dialog box.
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_creating_the_session_bean_test"><a class="anchor" href="#_creating_the_session_bean_test"></a>Creating the Session Bean Test</h2>
<div class="sectionbody">
<div class="paragraph">
<p>In this section you will create a JUnit test class for the <code>MyEntityFacade</code> session facade. The IDE will generate skeleton test methods for each of the methods in the facade class as well as each of the methods in the abstract facade. You will annotate the test methods that are generated for the methods in the abstract facade to instruct the IDE and the JUnit test runner to ignore them. You will then modify the test method for the <code>verify</code> method that you added to <code>MyEntityFacade</code> .</p>
</div>
<div class="paragraph">
<p>In the generated tests you will see that the IDE automatically adds code that calls on <code>EJBContainer</code> to create an instance of the EJB container.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click <code>MyEntityFacade.java</code> in the Projects window and choose Tools &gt; Create Tests.</p>
</li>
<li>
<p>Select a test framework from the Framework dropdown list</p>
</li>
<li>
<p>Use the default options in the Create Tests dialog box. Click OK.</p>
</li>
</ol>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
The first time that you create a JUnit test you need to specify the version of the JUnit framework. Select JUnit 4.x as the JUnit version and click Select.
</td>
</tr>
</table>
</div>
<div class="paragraph">
<p>By default, the IDE generates a skeleton test class that contains tests for each of the methods in <code>MyEntityFacade</code> and <code>AbstractFacade</code> . The IDE automatically adds a dependency on the JUnit 4.10 to the POM.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Annotate each of the test methods except <code>testVerify</code> with the <code>@Ignore</code> annotation. The IDE will skip each of the tests annotated with <code>@Ignore</code> when running the tests.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>Alternatively, you can delete all the test methods except <code>testVerify</code> .</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Locate the <code>testVerify</code> test method in the test class.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>You can see that the test contains a line that calls on <code>EJBContainer</code> .</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java"> @Test
public void testVerify() throws Exception {
System.out.println("verify");
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
MyEntityFacade instance = (MyEntityFacade)container.getContext().lookup("java:global/classes/MyEntityFacade");
int expResult = 0;
int result = instance.verify();
assertEquals(expResult, result);
container.close();
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Make the following changes (in bold) to the skeleton of the <code>testVerify</code> test method.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">@Test
public void testVerify() throws Exception {
System.out.println("verify");
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
MyEntityFacade instance = (MyEntityFacade)container.getContext().lookup("java:global/classes/MyEntityFacade");
*System.out.println("Inserting entities...");
instance.insert(5);*
int result = instance.verify();
*System.out.println("JPA call returned: " + result);
System.out.println("Done calling EJB");
Assert.assertTrue("Unexpected number of entities", (result == 5));*
container.close();
}</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Fix the import statements to add <code>junit.framework.Assert</code> . Save your changes.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>You now need to modify the POM to add a dependency on the <code>&lt;glassfish.embedded-static-shell.jar&gt;</code> that is located in your local installation of the GlassFish server.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Open <code>pom.xml</code> in the editor and locate the <code>&lt;properties&gt;</code> element.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"> &lt;properties&gt;
&lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;/properties&gt;</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Edit the <code>&lt;properties&gt;</code> element to add the <code>&lt;glassfish.embedded-static-shell.jar&gt;</code> element (in <strong>bold</strong>) that specifies the location of the JAR in your local GlassFish installation. You will then reference this property in the dependency on the artifact.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"> &lt;properties&gt;
&lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
*&lt;glassfish.embedded-static-shell.jar&gt;_&lt;INSTALLATION_PATH&gt;_/glassfish-4.0/glassfish/lib/embedded/glassfish-embedded-static-shell.jar&lt;/glassfish.embedded-static-shell.jar&gt;*
&lt;/properties&gt;</code></pre>
</div>
</div>
<div class="admonitionblock note">
<table>
<tr>
<td class="icon">
<i class="fa icon-note" title="Note"></i>
</td>
<td class="content">
<code><em>&lt;INSTALLATION_PATH&gt;</em></code> is the absolute path to your local GlassFish installation. You will need to modify this element in the POM if the path to the local installation changes.
</td>
</tr>
</table>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Right-click the Dependencies node in the Projects window and choose Add Dependency.</p>
</li>
<li>
<p>In the Add Dependency dialog box, type <strong>embedded-static-shell</strong> in the Query text field.</p>
</li>
<li>
<p>Locate the 4.0 JAR in the search results and click Add.</p>
</li>
</ol>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/add-shell-dependency.png" alt="add shell dependency">
</div>
<div class="title">Figure 5. Test Results window</div>
</div>
<div class="paragraph">
<p>When you click Add the IDE adds the dependency to the POM.</p>
</div>
<div class="paragraph">
<p>You now want to modify the POM to specify the local installation of GlassFish as the source for the JAR.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>Locate the dependency in the POM and make the following changes (in <strong>bold</strong>) to modify the element to reference the <code>&lt;glassfish.embedded-static-shell.jar&gt;</code> property that you added and to specify the <code>&lt;scope&gt;</code> . Save your changes.</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-xml hljs" data-lang="xml"> &lt;dependency&gt;
&lt;groupId&gt;org.glassfish.main.extras&lt;/groupId&gt;
&lt;artifactId&gt;glassfish-embedded-static-shell&lt;/artifactId&gt;
&lt;version&gt;4.0&lt;/version&gt;
*&lt;scope&gt;system&lt;/scope&gt;
&lt;systemPath&gt;${glassfish.embedded-static-shell.jar}&lt;/systemPath&gt;*
&lt;/dependency&gt;</code></pre>
</div>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the Services window, right-click the GlassFish Server node and choose Start.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>The JavaDB database server will also start when you start the GlassFish Server.</p>
</div>
<div class="olist arabic">
<ol class="arabic">
<li>
<p>In the Projects window, right-click the project node and choose Test.</p>
</li>
</ol>
</div>
<div class="paragraph">
<p>When you choose Test the IDE will build application and run the test phase of the build lifecycle. The unit tests will be executed with the surefire plugin, which supports running JUnit 4.x tests. For more about the surefire plugin, see <a href="http://maven.apache.org/plugins/maven-surefire-plugin/">http://maven.apache.org/plugins/maven-surefire-plugin/</a>.</p>
</div>
<div class="paragraph">
<p>You can see the results of the test in the Test Results window. You can open the Test Results window by choosing Window &gt; Output &gt; Test Results from the main menu.</p>
</div>
<div class="imageblock">
<div class="content">
<img src="../../../../_images/kb/docs/javaee/maven-test-results.png" alt="maven test results">
</div>
<div class="title">Figure 6. Test Results window</div>
</div>
<div class="paragraph">
<p>In the Test Results window you can click the Show Passed icon (images:./test-ok_16.png[title="Show Passed icon"]) to display a list of all the tests that passed. In this example you can see that nine tests passed. If you look in the Output window you can see that only one test was run and eight tests were skipped. Skipped tests are included in the list of tests that passed in the Test Results window.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">Running com.mycompany.mavenwebtestapp.MyEntityFacadeTest
verify
...
Inserting entities...
Inserting # 1
Inserting # 2
Inserting # 3
Inserting # 4
Inserting # 5
Found: Entity number 2 created at Wed Oct 09 19:06:59 CEST 2013
Found: Entity number 4 created at Wed Oct 09 19:06:59 CEST 2013
Found: Entity number 3 created at Wed Oct 09 19:06:59 CEST 2013
Found: Entity number 1 created at Wed Oct 09 19:06:59 CEST 2013
Found: Entity number 5 created at Wed Oct 09 19:06:59 CEST 2013
JPA call returned: 5
Done calling EJB
...
Results :
Tests run: 9, Failures: 0, Errors: 0, Skipped: 8</code></pre>
</div>
</div>
<div class="paragraph">
<p><a href="../../../../../../front/main/community/mailing-lists/" class="xref page">Send Feedback on This Tutorial</a></p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_see_also"><a class="anchor" href="#_see_also"></a>See Also</h2>
<div class="sectionbody">
<div class="paragraph">
<p>For more information about using NetBeans IDE to develop Java EE applications, see the following resources:</p>
</div>
<div class="ulist">
<ul>
<li>
<p><a href="../javaee-intro/" class="xref page">Introduction to Java EE Technology</a></p>
</li>
<li>
<p><a href="../javaee-gettingstarted/" class="xref page">Getting Started with Java EE Applications</a></p>
</li>
<li>
<p><a href="../maven-entapp/" class="xref page">Creating an Enterprise Application Using Maven</a></p>
</li>
<li>
<p><a href="../../java-ee/" class="xref page">Java EE &amp;amp; Java Web Learning Trail</a></p>
</li>
</ul>
</div>
<div class="paragraph">
<p>You can find more information about using Enterprise Beans in the <a href="http://download.oracle.com/javaee/6/tutorial/doc/">Java EE 6 Tutorial</a>.</p>
</div>
<div class="paragraph">
<p>To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, <a href="../../../../../../front/main/community/mailing-lists/" class="xref page">join the nbj2ee mailing list</a>.</p>
</div>
</div>
</div>
<section class='tools'>
<ul class="menu align-center">
<li><a title="Facebook" href="https://www.facebook.com/NetBeans"><i class="fa fa-md fa-facebook"></i></a></li>
<li><a title="Twitter" href="https://twitter.com/netbeans"><i class="fa fa-md fa-twitter"></i></a></li>
<li><a title="Github" href="https://github.com/apache/netbeans"><i class="fa fa-md fa-github"></i></a></li>
<li><a title="YouTube" href="https://www.youtube.com/user/netbeansvideos"><i class="fa fa-md fa-youtube"></i></a></li>
<li><a title="Atom Feed" href="https://netbeans.apache.org/blogs/atom"><i class="fa fa-mf fa-rss"></i></a></li>
<li><a title="Slack" href="https://tinyurl.com/netbeans-slack-signup/"><i class="fa fa-md fa-slack"></i></a></li>
<li><a title="Issues" href="https://github.com/apache/netbeans/issues"><i class="fa fa-mf fa-bug"></i></a></li>
</ul>
<ul class="menu align-center">
<li><a href="https://github.com/apache/netbeans-antora-tutorials/edit/main/modules/ROOT/pages/kb/docs/javaee/maven-entapp-testing.adoc" title="See this page in github"><i class="fa fa-md fa-edit"></i> See this page in GitHub.</a></li>
</ul>
</section>
</article>
</div>
<div class='grid-container incubator-area' style='margin-top: 64px'>
<div class='grid-x grid-padding-x'>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/">
<img style="height: 60px" title="Apache Software Foundation" src="../../../../../../_/images/asf_logo_wide.svg" />
</a>
</div>
<div class='large-auto cell text-center'>
<a href="https://www.apache.org/events/current-event.html">
<img style="width:234px; height: 60px;" title="Apache Software Foundation current event" src="https://www.apache.org/events/current-event-234x60.png"/>
</a>
</div>
</div>
</div>
<footer>
<div class="grid-container">
<div class="grid-x grid-padding-x">
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/about">About</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/who">Who's Who</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="https://www.apache.org/security/">Security</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/community">Community</a></h1>
<ul>
<li><a href="../../../../../../front/main/community/mailing-lists">Mailing lists</a></li>
<li><a href="../../../../../../front/main/community/committer">Becoming a committer</a></li>
<li><a href="../../../../../../front/main/community/events">NetBeans Events</a></li>
<li><a href="https://www.apache.org/events/current-event.html">Apache Events</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/participate">Participate</a></h1>
<ul>
<li><a href="../../../../../../front/main/participate/submit-pr">Submitting Pull Requests</a></li>
<li><a href="../../../../../../front/main/participate/report-issue">Reporting Issues</a></li>
<li><a href="../../../../../../front/main/participate/#documentation">Improving the documentation</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/help">Get Help</a></h1>
<ul>
<li><a href="../../../../../../front/main/help/#documentation">Documentation</a></li>
<li><a href="../../../../../../wiki/main/wiki">Wiki</a></li>
<li><a href="../../../../../../front/main/help/#support">Community Support</a></li>
<li><a href="../../../../../../front/main/help/commercial-support">Commercial Support</a></li>
</ul>
</div>
<div class="large-auto cell">
<h1><a href="../../../../../../front/main/download">Download</a></h1>
<ul>
<li><a href="../../../../../../front/main/download">Releases</a></li>
<li><a href="https://plugins.netbeans.apache.org/">Plugins</a></li>
<li><a href="../../../../../../front/main/download/#_daily_builds_and_building_from_source">Building from source</a></li>
<li><a href="../../../../../../front/main/download/#_older_releases">Previous releases</a></li>
</ul>
</div>
</div>
</div>
</footer>
<div class='footer-disclaimer'>
<div class="footer-disclaimer-content">
<p>Copyright &copy; 2017-2025 <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Licensed under the Apache <a href="https://www.apache.org/licenses/">license</a>, version 2.0</p>
<div style='max-width: 40em; margin: 0 auto'>
<p>Apache, Apache NetBeans, NetBeans, the Apache feather logo and the Apache NetBeans logo are trademarks of <a href="https://www.apache.org">The Apache Software Foundation</a>.</p>
<p>Oracle and Java are registered trademarks of Oracle and/or its affiliates.</p>
<p>The Apache NetBeans website conforms to the <a href="https://privacy.apache.org/policies/privacy-policy-public.html">Apache Software Foundation Privacy Policy</a></p>
</div>
</div>
</div>
<script src="../../../../../../_/js/vendor/lunr.js"></script>
<script src="../../../../../../_/js/search-ui.js" id="search-ui-script" data-site-root-path="../../../../../.." data-snippet-length="100" data-stylesheet="../../../../../../_/css/search.css"></script>
<script async src="../../../../../../search-index.js"></script>
<script src="../../../../../../_/js/vendor/jquery.min.js"></script>
<script src="../../../../../../_/js/vendor/what-input.min.js"></script>
<script src="../../../../../../_/js/vendor/foundation.min.js"></script>
<script src="../../../../../../_/js/vendor/jquery.colorbox-min.js"></script>
<script src="../../../../../../_/js/netbeans.js"></script>
<script>
$(function(){ $(document).foundation(); });
</script>
<script src="../../../../../../_/js/vendor/highlight.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code').forEach((el) => {
hljs.highlightElement(el);
});
});
</script>
</body>
</html>