如何对Java EE代码进行单元测试? [英] How do you unit test Java EE code?

查看:159
本文介绍了如何对Java EE代码进行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下您测试Java EE代码的首选方法吗?

I want to ask for your prefered way to test Java EE code?

我发现只有三个项目,它们试图帮助在Java EE中编写单元测试代码环境:

I found only three project, that are trying to help to code unit tests in Java EE environment:

  • http://jakarta.apache.org/cactus/ : Last Published: 2009-01-18
  • http://www.junitee.org/ : Last Release: 2004-12-11
  • http://ejb3unit.sourceforge.net/ : Last Release: 2008-05-17

所以我想知道,


  • 是否有任何框架有助于编写(j)Java EE代码的单元测试?

  • 你使用嵌入式Java EE服务器,比如jboss或glassfish v3吗?

  • 你自己做模型和注入吗?

非常感谢...

推荐答案

如果通过单元测试你的意思是......单元测试(单独测试一个单元),然后你实际上不需要任何特定的框架,因为EJB3.0只不过是带注释的POJO,因此可以在没有任何特殊夹具的情况下相对容易地进行测试。

If by Unit Testing you mean... unit testing (testing a unit in isolation), then you actually don't need any particular framework since EJB3.0 are nothing more than annotated POJOs and thus can be relatively easily tested without any special fixture.

现在,如果您指的是其他内容 - 例如集成测试功能测试 - 那么,,工具可以帮助并简化了一些事情(但你应该开始使用正确的术语:)我会假设这是你的想法。

Now, if you mean something else - like Integration Testing or Functional Testing - then, yes, tools can help and simplify things (but you should really start to use the right terminology :) I'll assume that this is what you have in mind.

首先, JUnitEE 似乎已经过时而且过时了,我甚至不确定它对EJB3.x有什么作用。第二,我对 Java EE 5支持印象不深=http://jakarta.apache.org/cactus/ =noreferrer> Cactus 并且不得不部署Cactus测试很痛苦(我认为Cactus对于J2EE 1.4来说很不错,但现在有点过时了)。所以这给我们留下了 Ejb3Unit ,这在我看来是最好的选择,特别是如果你想要运行超出容器测试,即没有真正部署应用程序(更快)。

First, JUnitEE seems dead and obsolete and I'm not even sure it has anything for EJB3.x. Second, I'm not impressed by the Java EE 5 support of Cactus and having to deploy Cactus tests is painful (I think that Cactus was nice for J2EE 1.4 but is a bit outdated now). So this leaves us with Ejb3Unit which is in my opinion the best option, especially if you want to run out of container tests i.e. without really deploying the application (much faster).

如果您想在容器测试中运行,那么您确实可以使用嵌入式容器,我当前的首选项也会转到GlassFish v3,即使对于Java EE 5(我可能错了,但我对最新的JBoss版本的开始时间感到非常失望,所以它没有引起我的注意)。请参阅 GlassFish Embedded Reloaded,口袋中的应用程序服务器以获取示例代码(您可以从您的测试中使用)或使用maven插件获取v3嵌入式玻璃鱼(如果您使用的是maven )。

If you want to run in container tests, then you could indeed use an embedded container and my current preference goes to GlassFish v3, even for Java EE 5 (I may be wrong but I'm pretty disappointed by the starting time of the latest JBoss releases so it isn't getting much of my attention). See the post GlassFish Embedded Reloaded, an appserver in your pocket for sample code (that you could use from your tests) or Using maven plugin for v3 embedded glassfish (if you are using maven).

另一种选择是使用打包和部署您的应用程序。货物然后对部署的应用程序运行一些测试(例如使用Selenium或BDD工具)。如果您希望使用不提供任何嵌入式API的容器运行端到端测试,这可能很有用。

Another option would be to package and deploy your application with Cargo and then run some tests against the deployed application (with Selenium or a BDD tool for example). This could be useful if you want to run end-to-end tests with a container that doesn't provide any embedded API.

所以,回答上一个问题,我确实会使用可用的工具,也许是它们的组合,用于不是单元测试的测试,也不会自己模拟/注入东西,除非它们不能满足我现在想不到的一些需求。

So, to answer your last question, I would indeed use available tools, maybe a combination of them, for tests that are not unit tests and wouldn't mock/inject stuff myself, except if they don't cover some needs that I can't think of right now.

这篇关于如何对Java EE代码进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆