EJB 单元测试

发布于 2024-08-06 07:10:07 字数 156 浏览 2 评论 0 原文

我正在寻找一种将 TDD 应用于会话 Bean 的方法。

任何人都可以提供有关如何对它们进行单元测试的建议和链接吗?

如何使用 JUnit 来做到这一点?

PS:我是测试驱动开发和会话 Bean 的新手。

我正在使用 EJB v2。

I'm looking for a way to apply TDD to Session Beans.

can anyone provide advices and links on how to unit test them ?

how to use JUnit to do so ?

P.S : I'm new to Test Driven Development and Session Beans.

I'm using EJB v2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-08-13 07:10:07

我假设您正在谈论 EJB2.x 会话 Bean。对于这些类型的动物,我喜欢做的是:

  • 使用会话 Bean 作为包装器,将逻辑委托给 POJO,您可以在容器外轻松测试该 POJO。外部容器测试更好、更快、更容易等,但不会涵盖部署描述符验证等内容-和/或-
  • 使用类似Cactus 用于容器内测试(检查 Howto EJB 文档)- 和/或 -
  • 使用 Cargo 用于集成测试。

I'm assuming you are talking about EJB2.x Session Beans. For these kind of animals, what I like to do is:

  • Use the Session Bean as a wrapper that just delegates logic to a POJO that you can test easily outside a container. Outside container testing is better, faster, easier, etc but won't cover things such as deployment descriptor validation - and/or -
  • Use something like Cactus for in-container testing (check the Howto EJB documentation) - and/or -
  • Build and deploy your EJB module with Cargo for integration testing.
黒涩兲箜 2024-08-13 07:10:07

您没有说明您正在使用哪个版本的 EJB。如果是 EJB v3,请查看 Ejb3Unit。来自网站:

Ejb3Unit 是一个 JUnit 扩展,可以执行自动化的独立 junit
对所有符合 Java EE 的 EJB 3.0 进行测试
项目。
容器外测试方法可以缩短构建测试周期,
因为没有容器部署
不再需要了。

不过,我建议将功能与 EJB 特定的功能分开。这将允许您在容器外部测试复杂的功能,而无需使用上述框架。大多数测试将测试 POJO(普通的旧 Java 对象),相对较少的测试将重点关注持久性框架测试。

编辑:因此,如果您使用 EJB v2,那么显然可以忽略第一点。然而,第二点仍然有效。

You don't say which version of EJB you're using. If it's EJB v3, check out Ejb3Unit. From the website:

Ejb3Unit is a JUnit extention and can execute automated standalone junit
tests for all EJB 3.0 conform Java EE
projects.
The out of container test approach leads to short build-test-cycles,
because no container deployment is
necessary anymore.

However I would advocate separating out functionality from the EJB-specifics. This will allow you to test complex functionality outside the container and without using frameworks such as the above. The majority of your tests would be testing POJOs (plain old Java objects) and relatively few would focus on your persistence framework testing.

EDIT: So if you're using EJB v2 then obviously ignore the first point. The second point remains valid, however.

じее 2024-08-13 07:10:07

我目前正在使用 apache openejb 作为单元测试的嵌入式容器。尽管这是一个 EJB3/JPA 项目,但它对于 EJB2 应该是相同的。要在测试中引导容器,您只需创建一个 InitialContext 对象,稍后可以使用该对象来查找 EJB 和数据源:

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

// a DataSource named "mysql"
props.put("mysql", "new://Resource?type=DataSource");
props.put("mysql.JdbcDriver", "com.mysql.jdbc.Driver");
props.put("mysql.JdbcUrl", "jdbc:mysql://localhost:3306");
props.put("mysql.JtaManaged", "true");
props.put("mysql.DefaultAutoCommit", "false");
props.put("mysql.UserName", "root");
props.put("mysql.Password", "root");

Context context = new InitialContext(props);
LocalInterface local = (LocalInterface)context.lookup(localInterfaceName + "BeanLocal");
DataSource ds = (DataSource)context.lookup("java:openejb/Resource/mysql");

编辑http://openejb.apache.org/3.0/index.html

I'm currently using apache openejb as an embedded container for unit tests. Although this is an EJB3 / JPA project it should work the same for EJB2. To bootstrap the container in your tests you just have to create an InitialContext object that you can later use to lookup EJBs and DataSources:

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");

// a DataSource named "mysql"
props.put("mysql", "new://Resource?type=DataSource");
props.put("mysql.JdbcDriver", "com.mysql.jdbc.Driver");
props.put("mysql.JdbcUrl", "jdbc:mysql://localhost:3306");
props.put("mysql.JtaManaged", "true");
props.put("mysql.DefaultAutoCommit", "false");
props.put("mysql.UserName", "root");
props.put("mysql.Password", "root");

Context context = new InitialContext(props);
LocalInterface local = (LocalInterface)context.lookup(localInterfaceName + "BeanLocal");
DataSource ds = (DataSource)context.lookup("java:openejb/Resource/mysql");

Edit: There is some more documentation in the 'Testing Techniques' section at http://openejb.apache.org/3.0/index.html.

浮华 2024-08-13 07:10:07

Mockrunner 可以与 MockEJB 结合使用,为基于 EJB 的应用程序编写测试。
看看这个
http://mockrunner.sourceforge.net/examplesejb.html

Mockrunner can be used in conjunction with MockEJB to write tests for EJB based applications.
Have a look at this
http://mockrunner.sourceforge.net/examplesejb.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文