blob: 76c1d8e0d6799cecca178c47d59acfe87864d5e6 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>
<title>使用嵌入的 EJB 容器测试企业应用程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<meta name="description" content="A tutorial on how to use the embedded EJB container to test a session bean and entity class.">
<meta name="KEYWORDS" content="NetBeans, embedded, EJB, container, GlassFish, JUnit, Tutorial">
<link rel="stylesheet" href="../../../netbeans.css">
<meta name="author" content="ken ganfield">
</head>
<body>
<h1>使用嵌入的 EJB 容器测试企业应用程序</h1>
<p>本教程说明了如何为 Java EE 企业应用程序创建和运行 JUnit 测试。在本教程中,将创建一个包含实体类和会话 Bean 的 Web 应用程序。首先,将为会话 Bean 创建一个 JUnit 测试类,并在嵌入的 EJB 容器中运行该测试。然后,在项目中添加一个实体类,并修改测试类以便为该实体类添加一个测试方法。</p>
<p><b>教程练习</b></p>
<img alt="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0" class="stamp" src="../../../images_www/articles/73/netbeans-stamp-80-74-73.png" title="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0">
<ul>
<li><a href="#Exercise_1">测试会话 Bean</a>
<ul>
<li><a href="#Exercise_1a">创建项目</a></li>
<li><a href="#Exercise_1b">创建会话 Bean</a></li>
<li><a href="#Exercise_1c">测试会话 Bean</a></li>
<li><a href="#Exercise_1d">修改测试以指定容器属性</a></li>
<li><a href="#Exercise_1e">使用 @BeforeClass 和 @AfterClass 标注</a></li>
</ul>
</li>
<li><a href="#Exercise_2">测试实体类</a>
<ul>
<li><a href="#Exercise_2a">创建实体类</a></li>
<li><a href="#Exercise_2b">修改会话 Bean</a></li>
<li><a href="#Exercise_2c">测试实体类</a></li>
</ul>
</li>
<li><a href="#Exercise_3">下载解决方案项目</a></li>
<!-- <li><a href="#Exercise_4">Troubleshooting</a></li>-->
</ul>
<p><b>要学习本教程,您需要具备以下软件和资源。</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">软件或资源</th>
<th class="tblheader" scope="col">要求的版本</th>
</tr>
<tr>
<td class="tbltd1"><a href="/downloads/">NetBeans IDE</a></td>
<td class="tbltd1">7.2、7.3、7.4、8.0、Java EE 包</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java 开发工具包 (JDK)</a></td>
<td class="tbltd1">版本 7 或 8</td>
</tr>
<tr>
<td class="tbltd1">GlassFish Server Open Source Edition</td>
<td class="tbltd1">3.1.x 或 4.x</td>
</tr>
</tbody>
</table>
<p class="notes"><strong>注:</strong></p>
<ul>
<li>本教程需要使用 JUnit 插件。如果在安装 IDE 时未安装 JUnit 插件,请打开 "Plugins"(插件)管理器,选择 "Available plugins"(可用插件)标签,然后安装 JUnit 插件。</li>
</ul>
<p><b>先决条件</b></p>
<p>本文档假定您具备以下技术的一些基本知识或编程经验:</p>
<ul>
<li>Java 编程</li>
<li>NetBeans IDE</li>
</ul>
<p>在开始本教程之前,您可能需要熟悉以下文档。</p>
<ul>
<li><a href="javaee-gettingstarted.html">Java EE 应用程序入门指南</a></li>
<li><a href="../java/junit-intro.html">在 NetBeans IDE 中编写 JUnit 测试</a></li>
<li><a href="http://download.oracle.com/docs/cd/E19798-01/821-1754/gjlde/index.html">将 EJB 3.1 可嵌入 API 与嵌入的 GlassFish Server 一起使用</a></li>
</ul>
<p class="tips">您可以下载<a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJavaEE%252FWebAppJUnit.zip">已完成项目的 zip 档案文件</a></p>
<!-- ===================================================================================== -->
<a name="Exercise_1"></a>
<!--Exercise 1: -->
<h2>测试会话 Bean</h2>
<p>在本节中,将创建一个包含会话 Bean 和实体类的简单 Java EE Web 应用程序。</p>
<div class="indent">
<a name="Exercise_1a"></a>
<h3>创建项目</h3>
<ol>
<li>从主菜单中选择 "File"(文件)> "New Project"(新建项目)(Ctrl-Shift-N 组合键;在 Mac 上为 ⌘-Shift-N 组合键)。</li>
<li>从 "Java Web" 类别中选择 "Web Application"(Web 应用程序)。单击 "Next"(下一步)。 </li>
<li>将项目命名为 <strong>WebAppJUnit</strong> 并设置项目位置。</li>
<li>取消选中 "Use Dedicated Folder"(使用专用文件夹)选项(如果该选项处于选中状态)。<br> 单击 "Next"(下一步)。</li>
<li>将服务器设置为 <strong>GlassFish Server</strong>,并将 Java EE 版本设置为 <strong>Java EE 6 Web</strong><strong>Java EE 7 Web</strong><br>单击 "Finish"(完成)。</li>
</ol>
<a name="Exercise_1b"></a>
<h3>创建会话 Bean</h3>
<p>在本练习中,将创建一个非常简单的会话 Bean,它包含一个添加两个数字的方法。</p>
<ol>
<li>在 "Projects"(项目)窗口中右键单击 "WebAppJUnit" 项目,然后选择 "New"(新建)> "Other"(其他)。</li>
<li>选择 "Enterprise JavaBeans" 类别中的 "Session Bean"(会话 Bean)。单击 "Next"(下一步)。</li>
<li>键入 <strong>MyBean</strong> 作为 EJB 名称。</li>
<li>键入 <strong>bean</strong> 作为包名。</li>
<li>选择 "Stateless"(无状态)作为会话类型。单击 "Finish"(完成)。
<p>单击 "Finish"(完成),此时将在编辑器中打开新类。</p></li>
<li>在编辑器中,将以下 <tt>addNumbers</tt> 方法添加到类中。
<pre class="examplecode">
@Stateless
public class MyBean {
<strong>public int addNumbers(int numberA, int numberB) {
return numberA + numberB;
}</strong>
}</pre>
<p class="notes"><strong>注:</strong>在本教程中,无需添加 <tt>@LocalBean</tt> 标注或实现接口。默认情况下,当未显式指定视图时,Bean 将公开无界面视图。</p>
</li>
<li>保存所做的更改。</li>
</ol>
<a name="Exercise_1c"></a>
<h3>测试会话 Bean</h3>
<p>在本练习中,将为会话 Bean 创建一个测试类以测试 <tt>addNumbers</tt> 方法。IDE 可以根据目标类中的方法,生成新的测试类和框架测试方法。</p>
<ol>
<li>在 "Projects"(项目)窗口中右键单击 <tt>MyBean</tt> 类,然后选择 "Tools"(工具)> "Create Tests"(创建测试)。</li>
<li>在 "Frameworks"(框架)下拉列表中选择 "JUnit"。</li>
<li>在 "Create Tests"(创建测试)对话框中,使用默认值。单击 "OK"(确定)。<br> <img alt="&quot;Create Tests&quot;(创建测试)对话框" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/create-tests-dialog.png" title="&quot;Create Tests&quot;(创建测试)对话框">
<p class="notes"><strong>注:</strong>首次创建 JUnit 单元测试时,您需要指定 JUnit 版本。在 "Select JUnit Version"(选择 JUnit 版本)对话框中,选择 "JUnit 4.x",然后单击 "Select"(选择)。</p>
<p>单击 "OK"(确定)后,IDE 将生成 <tt>MyBeanTest.java</tt> 文件并在编辑器中打开该类。</p>
<p>在 "Projects"(项目)窗口中,您可以看到 IDE 在 "Test Packages"(测试包)节点下面生成了测试类。默认情况下,IDE 在测试类中生成一个框架测试方法,它通过调用 <tt>javax.ejb.embeddable.EJBContainer.createEJBContainer()</tt> 来创建 EJB 容器实例。<tt>createEJBContainer()</tt> 方法是 EJB 3.1 可嵌入 API 中包含的 <a href="http://download.oracle.com/javaee/6/api/javax/ejb/embeddable/EJBContainer.html"><tt>EJBContainer</tt></a> 类中的方法之一。</p>
<p>如果在 "Projects"(项目)窗口中展开 "Test Libraries"(测试库)节点,您可以看到 IDE 自动添加了 GlassFish Server(可嵌入容器)和 JUnit 4.x 作为测试库。如果展开 GlassFish Server 库,则可以看到该库包含 <tt>glassfish-embedded-static-shell.jar</tt></p>
<img alt="&quot;Projects&quot;(项目)窗口的屏幕快照" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/embedded-static-shell-jar.png" title="&quot;Projects&quot;(项目)窗口中的项目结构">
<p class="notes"><strong>注:</strong><tt>glassfish-embedded-static-shell.jar</tt> JAR 不包含嵌入的 EJB 容器的源代码。<tt>glassfish-embedded-static-shell.jar</tt> JAR 要求在本地安装 GlassFish。本地 GlassFish 安装的类路径是由项目的目标服务器确定的。您可以在项目的 "Properties"(属性)对话框中更改目标服务器。</p>
</li>
<li>修改生成的框架测试方法以指定 <tt>numberA</tt><tt>numberB</tt><tt>expResult</tt> 的值,然后删除会失败的默认调用。
<pre class="examplecode">@Test
public void testAddNumbers() throws Exception {
System.out.println("addNumbers");
<strong>int numberA = 1;
int numberB = 2;</strong>
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
MyBean instance = (MyBean)container.getContext().lookup("java:global/classes/MyBean");
<strong>int expResult = 3;</strong>
int result = instance.addNumbers(numberA, numberB);
assertEquals(expResult, result);
container.close();
}</pre>
</li>
<li>在 "Projects"(项目)窗口中右键单击项目,然后选择 "Test"(测试)。</li>
</ol>
<p>运行测试时,将在 IDE 中打开 "Test Results"(测试结果)窗口并显示测试进度和结果。</p>
<img alt="&quot;Test Results&quot;(测试结果)窗口" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/test-results1.png" title="&quot;Test Results&quot;(测试结果)窗口">
<p>将在 "Output"(输出)窗口中看到类似以下的内容。</p>
<pre class="examplecode">Testsuite: bean.MyBeanTest
addNumbers
...
Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 31.272 sec
------------- Standard Output ---------------
addNumbers
...
------------- ---------------- ---------------
test-report:
test:
BUILD SUCCESSFUL (total time: 35 seconds)</pre>
<a name="Exercise_1d"></a>
<h3>修改测试以指定容器属性</h3>
<p>使用创建测试向导时,IDE 生成一个默认框架测试类,它包含用于启动 EJB 容器的代码。在本练习中,将修改用于启动该容器的生成代码,以便为嵌入的容器实例指定其他属性。</p>
<ol>
<li>将以下代码(粗体)添加到测试类中。
<pre class="examplecode">@Test
public void testAddNumbers() throws Exception {
System.out.println("addNumbers");
int numberA = 1;
int numberB = 2;
// Create a properties map to pass to the embeddable container:
<strong>Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();</strong>
// Use the MODULES property to specify the set of modules to be initialized,
// in this case a java.io.File <!--representing an ejb-jar or exploded ejb-jar directory-->
<strong>properties.put(EJBContainer.MODULES, new File("build/jar"));</strong>
// Create the container instance, passing it the properties map:
EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer(<strong>properties</strong>);
// Create the instance using the container context to look up the bean
// in the directory that contains the built classes
MyBean instance = (MyBean) container.getContext().lookup("java:global/classes/MyBean");
int expResult = 3;
// Invoke the addNumbers method on the bean instance:
int result = instance.addNumbers(numberA, numberB);
assertEquals(expResult, result);
// Close the embeddable container:
container.close();
}</pre></li>
<li>在编辑器中右键单击,然后选择 "Fix Imports"(修复导入)(Alt-Shift-I 组合键;在 Mac 上为 ⌘-Shift-I 组合键)添加 <tt>java.util.HashMap</tt><tt>java.util.Map</tt> 的 import 语句。</li>
<li>再次运行测试,以确认修改的测试正常工作并且正确创建了容器。
<p class="tips"> 您可以在 "Test Results"(测试结果)窗口中单击 "Rerun"(重新运行)按钮。</p>
</li>
</ol>
<!-- TODO: need to check this
<p> If I do for example</p>
<pre class="examplecode">properties.put(EJBContainer.MODULES, new File("build/some-folder-with-classes"));</pre>
<p>then embeddable EJB container will use the folder
<tt>build/some-folder-with-classes</tt> to load EJBs from and the JNDI name to such
EJBs would be <tt>"java:global/some-folder-with-classes/&lt;my ejb name&gt;"</tt>. </p>
<p>Or I could set it to the following.</p>
<pre class="examplecode">properties.put(EJBContainer.MODULES, new File("build/my-archive.jar"));</pre>
<p>and lookup would be <tt>"java:global/my-archive/&lt;my ejb name&gt;"</tt> and only EJBs from
my-archive.jar would be loaded.</p>
<p>Just for the sake of example, if I wanted to use a single JNDI name regardless
of my EJB being in archive or a classes folder then I would have to create
ejb-jar.xml and set the module name to "my-module-name" and in unit test I could do</p>
<pre class="examplecode">properties.put(EJBContainer.MODULES, new File("build/some-folder-with-classes"));</pre>
<p>or</p>
<pre class="examplecode">properties.put(EJBContainer.MODULES, new File("build/my-archive.jar"));</pre>
<p>The JNDI name would be the same in both of the cases:<br>
<tt>"java:global/my-module-name/&lt;my ejb name&gt;"</tt></p>
<p>In the last example you could put this dir/jar in the classpath, skip the
property, and get the same result.
</p>
-->
<a name="Exercise_1e"></a>
<h3>使用 <tt>@BeforeClass</tt><tt>@AfterClass</tt> 标注</h3>
<p>在本练习中,将修改创建单个方法所需的测试类,以便创建和关闭容器实例。如果要运行几个可使用相同容器实例的测试,这可能是非常有用的。这样,您就不需要针对每个测试打开和关闭容器实例,只需在运行测试之前创建一个实例,并在完成所有测试后关闭该实例。</p>
<p>在本练习中,您需要将用于创建 EJB 容器的代码移到 <tt>setUpClass</tt> 方法中。<tt>setUpClass</tt> 方法是使用 <tt>@BeforeClass</tt> 标注的,用于指示在测试类中的其他方法运行之前将要运行的某个方法。在本示例中,将在 <tt>testAddNumbers</tt> 测试方法之前创建容器实例,并且该容器在关闭之前将一直存在。</p>
<p>同样,您需要将用于关闭该容器的代码移到 <tt>tearDownClass</tt> 方法中,该方法是使用 <tt>@AfterClass</tt> 标注的。</p>
<ol>
<li>将以下字段添加到测试类中。
<pre class="examplecode">private static EJBContainer container;</pre></li>
<li>将用于创建容器的代码从 <tt>testAddNumbers</tt> 测试方法复制到 <tt>setUpClass</tt> 方法和
<pre class=examplecode>@BeforeClass
public static void setUpClass() <strong>throws Exception</strong> {
<strong>Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
properties.put(EJBContainer.MODULES, new File("build/jar"));
container = EJBContainer.createEJBContainer(properties);
System.out.println("Opening the container");</strong>
}</pre>
</li>
<li>将用于关闭容器的代码从 <tt>testAddNumbers</tt> 测试方法复制到 <tt>tearDownClass</tt> 方法中。
<pre class=examplecode>@AfterClass
public static void tearDownClass() <strong>throws Exception</strong> {
<strong>container.close();
System.out.println("Closing the container");</strong>
}</pre>
</li>
<li><tt>testAddNumbers</tt> 方法中删除多余的代码。保存所做的更改。</li>
</ol>
<p>现在,测试类应如下所示。</p>
<pre class=examplecode>public class MyBeanTest {
private static EJBContainer container;
public MyBeanTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
Map&lt;String, Object&gt; properties = new HashMap&lt;String, Object&gt;();
properties.put(EJBContainer.MODULES, new File("build/jar"));
container = EJBContainer.createEJBContainer(properties);
System.out.println("Opening the container");
}
@AfterClass
public static void tearDownClass() throws Exception {
container.close();
System.out.println("Closing the container");
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of addNumbers method, of class MyBean.
*/
@Test
public void testAddNumbers() throws Exception {
System.out.println("addNumbers");
int numberA = 1;
int numberB = 2;
// Create the instance using the container context to look up the bean
// in the directory that contains the built classes
MyBean instance = (MyBean) container.getContext().lookup("java:global/classes/MyBean");
int expResult = 3;
// Invoke the addNumbers method on the bean instance:
int result = instance.addNumbers(numberA, numberB);
assertEquals(expResult, result);
}
}</pre>
<p>如果再次运行测试以确认正确创建和关闭了容器,则 "Test Results"(测试结果)窗口中将会显示类似下面的输出。</p>
<img alt="&quot;Test Results&quot;(测试结果)窗口" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/test-results2a.png" title="&quot;Test Results&quot;(测试结果)窗口">
<p>您可以看到在 <tt>addNumbers</tt> 测试之前运行了 <tt>setUpClass</tt> 方法并输出了 "Opening the container"。</p>
</div>
<!-- ===================================================================================== -->
<a name="Exercise_2"></a>
<h2>测试实体类</h2>
<p>在本节中,将创建一个实体类和持久性单元,并修改会话 Bean 以注入实体管理器和访问实体。在新实体类中,将添加一个简单方法以输出条目的 ID 号。然后,在会话 Bean 中添加一些简单方法以在数据库中创建和验证条目。
</p>
<div class="indent">
<a name="Exercise_2a"></a>
<h3>创建实体类</h3>
<p>在本节中,将通过新建实体类向导使用数据库连接详细信息创建一个实体类和持久性单元。
<ol>
<li>在 "Projects"(项目)窗口中右键单击 "WebAppJUnit" 项目,然后选择 "New"(新建)> "Other"(其他)。</li>
<li>在 "Persistence"(持久性)类别中选择 "Entity Class"(实体类)。单击 "Next"(下一步)。</li>
<li>在 "Class Name"(类名)中键入 <strong>SimpleEntity</strong></li>
<li>从 "Package"(包)下拉列表中选择 "Bean"。</li>
<li>在 "Primary Key Type"(主键类型)中键入 <strong>int</strong>。单击 "Next"(下一步)。</li>
<li>使用默认的持久性单元名称和持久性提供器。</li>
<li>选择 <tt>jdbc/sample</tt> 作为数据源,然后选 "Drop and Create"(删除并创建)作为策略。单击 "Finish"(完成)。<br> <img alt="&quot;Create Entity Class&quot;(创建实体类)对话框" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/create-entity-wizard.png" title="&quot;Create Entity Class&quot;(创建实体类)对话框">
<p>单击 "Finish"(完成),此时将在编辑器中打开新的实体类。如果在 "Projects"(项目)窗口中展开 "Configuration Files"(配置文件)节点,则可以看到 IDE 自动生成了 <tt>persistence.xml</tt> 文件,该文件定义了 <tt>WebAppJUnitPU</tt> 持久性单元的属性。</p></li>
<li>在编辑器中,将以下私有字段添加到实体类中。
<pre class="examplecode">private String name;</pre>
</li>
<li>在源代码编辑器中右键单击,选择 "Insert Code"(插入代码)(Alt-Insert 组合键;在 Mac 上为 Ctrl-I 组合键),然后选择 "Getter and Setter"(Getter 和 Setter),以打开 "Generate Getters and Setters"(生成 Getter 和 Setter)对话框。</li>
<li>在对话框中选择 <tt>name</tt> 字段。单击 "Generate"(生成)。<br />
<li>将以下方法添加到类中。
<pre class="examplecode">
public SimpleEntity(int id) {
this.id = id;
name = "Entity number " + id + " created at " + new Date();
}</pre>
</li>
<li>使用 <tt>@NamedQueries</tt><tt>@NamedQuery</tt> 标注来创建指定的 SQL 查询。
<pre class="examplecode">@Entity
<strong>@NamedQueries({@NamedQuery(name = "SimpleEntity.findAll", query = "select e from SimpleEntity e")})</strong>
public class SimpleEntity implements Serializable {</pre>
</li>
<li>创建一个默认构造函数。
<p class="tips">如果希望 IDE 生成构造函数,您可以单击类声明旁边的装订线中显示的建议图标。</p>
</li>
<li>修复导入以添加 <tt>javax.persistence.NamedQueries</tt><tt>javax.persistence.NamedQuery</tt><tt>java.util.Date</tt> 的 import 语句。保存所做的更改。</li>
</ol>
<p>除了默认的生成代码以外,实体类现在应类似于以下内容:</p>
<pre class="examplecode">package bean;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries({@NamedQuery(name = "SimpleEntity.findAll", query = "select e from SimpleEntity e")})
public class SimpleEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
public SimpleEntity() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SimpleEntity(int id) {
this.id = id;
name = "Entity number " + id + " created at " + new Date();
}
...
}</pre>
<a name="Exercise_2b"></a>
<h3>修改会话 Bean</h3>
<p>在本练习中,将编辑 <tt>MyBean</tt> 会话 Bean 以添加在数据库表中插入和检索数据的方法。</p>
<ol>
<li>在编辑器中打开 <tt>MyBean.java</tt></li>
<li>在编辑器中右键单击,选择 "Insert Code"(插入代码)(Alt-Insert 组合键;在 Mac 上为 Ctrl-I 组合键),然后从弹出式菜单中选择 "Use Entity Manager"(使用实体管理器)。
<p>选择 "Use Entity Manager"(使用实体管理器)时,IDE 将在类中添加以下代码以注入实体管理器。您可以看到自动生成了持久性单元的名称。</p>
<pre class="examplecode">@PersistenceContext(unitName="WebAppJUnitPU")
private EntityManager em;</pre>
</li>
<li>添加以下 <tt>verify</tt><tt>insert</tt> 方法。
<pre class="examplecode">@PermitAll
public int verify() {
String result = null;
Query q = em.createNamedQuery("SimpleEntity.findAll");
Collection entities = q.getResultList();
int s = entities.size();
for (Object o : entities) {
SimpleEntity se = (SimpleEntity)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);
SimpleEntity e = new SimpleEntity(i);
em.persist(e);
}
}</pre></li>
<li>修复导入以导入 <tt>javax.persistence.Query</tt> 并保存所做的更改。</li>
</ol>
<a name="Exercise_2c"></a>
<h3>测试实体类</h3>
<p>在本练习中,将编辑测试类以添加一个方法,测试应用程序是否可以查找 EJB 以及 <tt>insert</tt><tt>verify</tt> 方法是否正常工作。</p>
<ol>
<li>启动 JavaDB 数据库。</li>
<li>在编辑器中打开 <tt>MyBeanTest.java</tt> 测试类。</li>
<li>编辑该测试类以添加以下 <tt>testInsert</tt> 测试方法。
<pre class="examplecode">@Test
public void testInsert() throws Exception {
// Lookup the EJB
System.out.println("Looking up EJB...");
MyBean instance = (MyBean) container.getContext().lookup("java:global/classes/MyBean");
System.out.println("Inserting entities...");
instance.insert(5);
int res = instance.verify();
System.out.println("JPA call returned: " + res);
System.out.println("Done calling EJB");
Assert.assertTrue("Unexpected number of entities", (res == 5));
System.out.println("..........SUCCESSFULLY finished embedded test");
}</pre>
</li>
<!-- <li>Fix your imports to import <tt>javax.naming.Context</tt> and <tt>junit.framework.Assert</tt>.</li>-->
<li>在 "Projects"(项目)窗口中右键单击项目节点,然后从弹出式菜单中选择 "Test"(测试)。</li>
</ol>
<p>此时将打开 "Test Results"(测试结果)窗口并显示类似下面的输出。</p>
<img alt="&quot;Test Results&quot;(测试结果)窗口" class="margin-around b-all" src="../../../images_www/articles/72/javaee/junit-embedded-ant/test-results2b.png" title="添加 testInsert 测试后的 &quot;Test Results&quot;(测试结果)窗口">
<p>您可以通过添加到测试类中的输出消息查看测试进度和测试运行顺序。
</p>
</div>
现在您已为会话 Bean 创建了测试并知道实体类连接正常工作,因此可以开始对应用程序的 Web 接口进行编码。
<!-- ===================================================================================== -->
<a name="Exercise_3"></a>
<h2>下载解决方案项目</h2>
<p>您可以采用下列方法下载本教程的解决方案(作为一个项目)。</p>
<ul>
<li>下载<a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJavaEE%252FWebAppJUnit.zip">已完成项目的 zip 档案文件</a></li>
<li>通过执行以下步骤从 NetBeans 样例检出项目源代码:
<ol>
<li>从主菜单中选择 "Team"(团队开发)> "Subversion" > "Checkout"(检出)。</li>
<li>在 "Checkout"(检出)对话框中,输入以下资源库 URL:<br> <tt>https://svn.netbeans.org/svn/samples~samples-source-code</tt><br> 单击 "Next"(下一步)。</li>
<li>单击 "Browse"(浏览)以打开 "Browse Repository Folders"(浏览资源库文件夹)对话框。</li>
<li>展开根节点并选择 <strong>samples/javaee/WebAppJUnit</strong>。单击 "OK"(确定)。</li>
<li>指定用于存储源代码的本地文件夹(本地文件夹必须为空)。</li>
<li>单击 "Finish"(完成)。
<p>单击 "Finish"(完成),此时 IDE 会将本地文件夹初始化为 Subversion 资源库,并检出项目源代码。</p>
</li>
<li>在完成检出操作后将会显示一个对话框,在该对话框中单击 "Open Project"(打开项目)。</li>
</ol>
<p class="notes"><strong>注:</strong></p>
<ul>
<li>需要 Subversion 客户端检出源代码。有关安装 Subversion 的更多信息,请参见 <a href="../ide/subversion.html">NetBeans IDE 中的 Subversion 指南</a>中有关<a href="../ide/subversion.html#settingUp">设置 Subversion</a> 的部分。</li>
</ul>
</li>
</ul>
<!--<a name="Exercise_4"></a>
<h2>Troubleshooting</h2>
<p></p>-->
<!--<p>If you are using this tutorial on OS X, you may encounter the following problem when testing the project.</p>
<div class="indent">
<h3>OutOfMemory Exception</h3>
<p>To prevent an OutOfMemory (OOM) exception, you need to modify the build script.</p>
<ol>
<li>Expand the project in the Files window.</li>
<li>Expand the nbproject node and double-click <tt>build-impl.xml</tt> to open the file in the editor.</li>
<li>Search for macrodef-junit in the file.</li>
<li>Locate the classpath elements.
<pre class="examplecode">
&lt;macrodef name="junit" uri="https://netbeans.org/ns/web-project/2"&gt;
...
&lt;sequential&gt;
...
<strong>&lt;classpath&gt;
&lt;path path="${run.test.classpath}"/&gt;
&lt;path path="${j2ee.platform.classpath}"/&gt;
&lt;path path="${j2ee.platform.embeddableejb.classpath}"/&gt;
&lt;/classpath&gt;</strong>
...
&lt;sequential&gt;
...
&lt;macrodef name="junit" uri="https://netbeans.org/ns/web-project/2"&gt;
</pre></li>
<li>Comment out the following classpath element.
<pre class="examplecode">&lt;path path="${j2ee.platform.classpath}"/&gt;</pre>
</li>
<li>Save your changes.</li>
</ol>-->
<br>
<div class="feedback-box" ><a href="/about/contact_form.html?to=3&amp;subject=Feedback:%20Using%20the%20Embedded%20EJB%20Container">发送有关此教程的反馈意见</a></div>
<br style="clear:both;">
<!-- ======================================================================================= -->
<h2><a name="nextsteps"></a>另请参见</h2>
<p>有关使用 NetBeans IDE 开发 Java EE 应用程序的更多信息,请参见以下资源:
</p>
<ul>
<li><a href="javaee-intro.html">Java EE 技术简介</a></li>
<li><a href="javaee-gettingstarted.html">Java EE 应用程序入门指南</a></li>
<li><a href="../web/quickstart-webapps.html">Web 应用程序开发简介</a></li>
<li><a href="../../trails/java-ee.html">Java EE 和 Java Web 学习资源</a></li>
</ul>
<p>您可以在 <a href="http://download.oracle.com/javaee/6/tutorial/doc/">Java EE 6 教程</a>中找到有关使用 EJB 3.1 企业 Bean 的详细信息。</p>
<p>要发送意见和建议、获得支持以及随时了解 NetBeans IDE Java EE 开发功能的最新开发情况,请<a href="../../../community/lists/top.html">加入 nbj2ee 邮件列表</a></p>
</body>
</html>