您如何对 Java EE 代码进行单元测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1947846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:40:08  来源:igfitidea点击:

How do you unit test Java EE code?

javaunit-testingjunitejb-3.0java-ee-5

提问by marabol

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:

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

So I wonder,

所以我想知道,

  • is there any framework helping to write (j) unit test for Java EE code?
  • do you use embedded Java EE servers like jboss or glassfish v3?
  • do you mockup and inject by yourself?
  • 是否有任何框架有助于为 Java EE 代码编写 (j) 单元测试?
  • 您是否使用嵌入式 Java EE 服务器,例如 jboss 或 glassfish v3?
  • 你自己做模型和注入吗?

Thanks a lot...

非常感谢...

采纳答案by Pascal Thivent

If by Unit Testingyou 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.

如果单元测试是指...单元测试(孤立地测试一个单元),那么您实际上不需要任何特定的框架,因为 EJB3.0 只不过是带注释的 POJO,因此可以相对轻松地进行测试,无需任何特殊装置.

Now, if you mean something else - like Integration Testingor 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.

现在,如果你的意思是其他的——比如集成测试功能测试——那么,是的,工具可以帮助和简化事情(但你真的应该开始使用正确的术语:) 我会假设这就是你的想法.

First, JUnitEEseems 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 supportof Cactusand 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 Ejb3Unitwhich is in my opinion the best option, especially if you want to run out of containertests i.e. without really deploying the application (much faster).

首先,JUnitEE似乎已经过时并且过时了,我什至不确定它对 EJB3.x 有什么帮助。其次,我不是被打动的Java EE 5的支持仙人掌和无需部署仙人掌测试是痛苦的(我认为仙人掌是很好的J2EE 1.4,但现在已经过时了一下)。所以这给我们留下了Ejb3Unit,在我看来这是最好的选择,特别是如果你想用完容器测试,即没有真正部署应用程序(更快)。

If you want to run in containertests, 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 pocketfor sample code (that you could use from your tests) or Using maven plugin for v3 embedded glassfish(if you are using maven).

如果你想在容器测试中运行,那么你确实可以使用嵌入式容器,我目前的偏好是 GlassFish v3,即使是 Java EE 5(我可能错了,但我对最新的 JBoss 的开始时间感到非常失望发布所以它没有引起我的注意)。请参阅文章GlassFish Embedded Reloaded,您口袋中的应用程序服务器以获取示例代码(您可以在测试中使用)或为 v3 嵌入式 glassfish 使用 maven 插件(如果您使用的是 maven)。

Another option would be to package and deploy your application with Cargoand 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.

另一种选择是使用Cargo打包和部署您的应用程序,然后针对已部署的应用程序运行一些测试(例如使用 Selenium 或 BDD 工具)。如果您想使用不提供任何嵌入式 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.

所以,为了回答你的最后一个问题,我确实会使用可用的工具,也许是它们的组合,用于不是单元测试并且不会自己模拟/注入东西的测试,除非它们不满足我可以的一些需求暂时想不起来。

回答by Sundar

As you are interested in unit testing, I recommend JUnit. You can unit test the methods in the core classes. If you have difficulty in writing unit test cases using JUnit, then probably the design is not modular and it is highly coupled. First focus on your core functionality and test it using JUnit.

由于您对单元测试感兴趣,我推荐 JUnit。您可以对核心类中的方法进行单元测试。如果您在使用 JUnit 编写单元测试用例时遇到困难,那么很可能该设计不是模块化的并且是高度耦合的。首先关注您的核心功能并使用 JUnit 对其进行测试。

回答by Harald Wellmann

I've been facing the same problem of running integration tests based on JUnit in a Java EE 6 container (Glassfish v3, to be precise), and after a lot of browsing and searching, I could not find a solution that really suited me needs, so I wrote my own, now published as jeeuniton Google Code.

我一直面临着在 Java EE 6 容器(准确地说是 Glassfish v3)中运行基于 JUnit 的集成测试的同样问题,经过大量浏览和搜索,我找不到真正适合我需要的解决方案,所以我写了我自己的,现在作为jeeunit在谷歌代码上发布。

I wouldn't call it a test framework, it is really just a handful of classes providing the glue between JUnit and Embedded Glassfish.

我不会称它为测试框架,它实际上只是提供 JUnit 和嵌入式 Glassfish 之间的粘合剂的少数类。

The general idea is similar to Cactus, your tests run in the container and get triggered by a servlet from outside.

总体思路类似于 Cactus,您的测试在容器中运行并由外部的 servlet 触发。

jeeunit supports JUnit 4, Glassfish v3, CDI and generates the standard XML JUnit reports just like Ant or Maven Surefire (in fact, I reused some code from Ant for generating the reports).

jeeunit 支持 JUnit 4、Glassfish v3、CDI 并生成标准的 XML JUnit 报告,就像 Ant 或 Maven Surefire 一样(实际上,我重用了 Ant 的一些代码来生成报告)。

回答by Bryn

I had a requirement to test a CDI application and wrote a custom JUnit runner that runs everything outside of the web container.

我需要测试一个 CDI 应用程序,并编写了一个自定义 JUnit 运行器来运行 Web 容器之外的所有内容。

http://jglue.org/cdi-unit/

http://jglue.org/cdi-unit/

It is suitable for Java SE and also supports dummy Request, Session and Conversation scopes for testing web apps.

它适用于 Java SE,还支持用于测试 Web 应用程序的虚拟请求、会话和对话范围。

It's small and fast, which is great when you have lots of unit tests.

它又小又快,当你有很多单元测试时,这很好。