Java 当前最好的单元测试框架 EJB3 / JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2485635/
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
Best current framework for unit testing EJB3 / JPA
提问by Dark Castle
Starting a new project using EJB 3 / JPA, mainly stateless session beans and batch jobs. I've used JUnit in the past on standard Java webapps and it seemed to work pretty well. In EJB2 unit testing was a pain and required a running container such as JBoss to make the calls into. Now that we're going to be working in EJB3 / JPA I'd like to know what companies are using to write and run these tests. Are Junit and JMock still considered relevant or are there other newer frameworks that have come around that we should investigate?
使用 EJB 3 / JPA 启动一个新项目,主要是无状态会话 bean 和批处理作业。我过去曾在标准 Java webapps 上使用过 JUnit,它似乎工作得很好。在 EJB2 中,单元测试很痛苦,需要一个正在运行的容器(如 JBoss)来进行调用。现在我们将在 EJB3/JPA 中工作,我想知道公司正在使用什么来编写和运行这些测试。Junit 和 JMock 是否仍然被认为是相关的,或者是否有其他新的框架出现,我们应该调查?
采纳答案by ewernli
IMHO, yes they are still relevant.
恕我直言,是的,它们仍然相关。
With EJB3, you can test you EJB either like regular POJO, or as managed bean using an embedded EJB container.
使用 EJB3,您可以像常规 POJO 一样测试 EJB,也可以使用嵌入式 EJB 容器将 EJB 测试为托管 bean。
Same for JPA, you can either embed a JPA implementation for your test and use an in-memory database, or you can mock the data layer completely.
对于 JPA,您可以为测试嵌入 JPA 实现并使用内存数据库,也可以完全模拟数据层。
For my last EJB project I had written a bit of glue codeto test the EJB because embedded EJB container were not mature enough, but now I would go for an embedded EJB container + JUnit.
在我的上一个 EJB 项目中,我编写了一些胶水代码来测试 EJB,因为嵌入式 EJB 容器还不够成熟,但现在我会选择嵌入式 EJB 容器 + JUnit。
A few resources
一些资源
- Blog post: How to unit test EJB 3 in 0.8 seconds
- SO question: Good unit testing resource for EJB
- 博客文章:如何在 0.8 秒内对 EJB 3 进行单元测试
- SO 问题:EJB 的良好单元测试资源
But there are many others easily findable
但是还有很多其他的很容易找到
回答by Sunil Manheri
回答by Pascal Thivent
For unit testing, you can use JUnit, TestNG and your favorite mocking framework (EasyMock, Mockito, PowerMock, JMockit, JMock).
对于单元测试,您可以使用 JUnit、TestNG 和您最喜欢的模拟框架(EasyMock、Mockito、PowerMock、JMockit、JMock)。
For integration testing, you could start/stop an embeddable container (JBoss, OpenEJB, GlassFish v3) from your tests. Or, have a look at Arquillian, a very recent new player for integration testing of Java EE applications:
对于集成测试,您可以从测试中启动/停止可嵌入容器(JBoss、OpenEJB、GlassFish v3)。或者,看看Arquillian,这是一个用于 Java EE 应用程序集成测试的新玩家:
The mission of the Arquillian project is to provide a simple test harness that developers can use to produce a broad range of integration tests for their Java applications (most likely enterprise applications). A test case may be executed within the container, deployed alongside the code under test, or by coordinating with the container, acting as a client to the deployed code.
To avoid introducing unnecessary complexity into the developer's build environment, Arquillian integrates transparently with familiar testing frameworks (e.g. JUnit 4, TestNG 5), allowing tests to be launched using existing IDE, Ant and Maven test plugins without any add-ons.
Arquillian 项目的任务是提供一个简单的测试工具,开发人员可以使用它来为他们的 Java 应用程序(很可能是企业应用程序)生成广泛的集成测试。测试用例可以在容器内执行,与被测代码一起部署,或者通过与容器协调,充当已部署代码的客户端。
为了避免给开发人员的构建环境带来不必要的复杂性,Arquillian 与熟悉的测试框架(例如 JUnit 4、TestNG 5)透明地集成,允许使用现有的 IDE、Ant 和 Maven 测试插件启动测试,而无需任何附加组件。
Also check Improving the Testability of Java EE With Arquillian 1.0.0 Alpha 1 Releasedand the Arquillanspace. Looks interesting IMO.
另请查看使用 Arquillian 1.0.0 Alpha 1 Released和Arquillan空间提高 Java EE 的可测试性。看起来很有趣 IMO。
回答by fish
One option is also to use Spring, but of course your architecture will then be based on Spring on the runtime as well. The reason I like Spring is that it is easy to inject what ever mock data and objects is needed during the testing phase as you can annotate in the JUnit test case which context file to use:
一种选择也是使用 Spring,但当然,您的架构也将在运行时基于 Spring。我喜欢 Spring 的原因是它很容易注入在测试阶段需要的任何模拟数据和对象,因为您可以在 JUnit 测试用例中注释要使用的上下文文件:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml"})
public class MyClassTest {
@Autowired
private MyClass myClass;
@Test
public void testMyMethod() {....}
But if you don't want to use Spring anyway, then this is quite irrelevant :) On the other hand, it is very easy to switch out from Spring afterwards.
但是,如果您无论如何都不想使用 Spring,那么这完全无关紧要 :) 另一方面,之后很容易从 Spring 切换出去。
回答by Beijing Beijing
I would suggest Arquillian. With Arquillian you have the options to test normal EJBs, or CDI beans. You could run your test agains an enbedded, managed, or remote EE container. The Arquillian project is continuously maintanced...
我会建议 Arquillian。使用 Arquillian,您可以选择测试普通 EJB 或 CDI bean。您可以在嵌入式、托管或远程 EE 容器中再次运行您的测试。Arquillian 项目持续维护...
However it is not always so easy to make your first arquillian test run. You might have problem finding the dependencies for the correct arquillian version and container version. I have posted a tutorial serial about this. But to not make me suspious of promoting, the link is not posted here. If you like you could just google "Tutorial EJB3 Integration Test with Arquillian part1".
然而,让您的第一次 arquillian 测试运行并不总是那么容易。您可能无法找到正确的 arquillian 版本和容器版本的依赖项。我已经发布了一个关于这个的教程系列。但为了不让我怀疑推广,这里没有发布链接。如果您愿意,您可以在 google 上搜索“使用 Arquillian 第 1 部分进行的 EJB3 集成测试教程”。