Junit 测试 Spring 服务和 DAO 层
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8601536/
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
Junit to test Spring service and DAO layer
提问by xyz
I am working on a Spring application (Spring 3.0) and following layered architecturei.e. Controller -> Service -> DAO layers.
我正在开发一个 Spring 应用程序(Spring 3.0)并遵循分层架构,即Controller -> Service -> DAO layers.
I want to write unit test cases for service and DAO layer using Junit.
I checked Spring official site and also tried many other sites but couldn't figure out an easy and simple way of doing it.
我想使用Junit为服务和 DAO 层编写单元测试用例。
我查看了 Spring 官方网站并尝试了许多其他网站,但找不到一种简单易行的方法。
Can anybody provide me some helpful resources ?
有人能给我提供一些有用的资源吗?
EDIT :
Looks like Mockitois the good option. Any good link to use it in Spring.
编辑:
看起来Mockito是不错的选择。在 Spring 中使用它的任何好的链接。
Thank you Alex for suggesting it.
谢谢亚历克斯的建议。
采纳答案by Alex Barnes
In terms of resources the Spring documentation on testing is very good. This can be found here.
在资源方面,Spring 测试文档非常好。这可以在这里找到。
When you test your service layer you will want to use a mocking library such as Mockito to mock your DAOs and therefore your domain layer. This ensures that they are true unit tests.
当您测试您的服务层时,您将需要使用 Mockito 之类的模拟库来模拟您的 DAO,从而模拟您的域层。这确保它们是真正的单元测试。
Then to integration test your DAOs against a database you can use the Spring transactional test utilities described in that reference documentation.
然后要针对数据库对 DAO 进行集成测试,您可以使用该参考文档中描述的 Spring 事务测试实用程序。
回答by Pete
Don't know much about resources, but it's not that hard to do if you have your dao + spring setup nicely. You'll need the following:
对资源不太了解,但是如果你的 dao + spring 设置很好,这并不难。您将需要以下内容:
JUNIT dependencies obivously. with maven, something like that:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency>The test class, which you place inside src/test/java:
@TransactionConfiguration(defaultRollback = true) @ContextConfiguration({ "classpath:test-spring-context.xml" }) @Transactional @RunWith(SpringJUnit4ClassRunner.class) public class SomeTests { // ... }The context file for your spring setup referencing your DAO datasource placed inside src/test/resources. Somewhere in your test-spring-context.xml:
<import resource="datasource-test.xml" />
显然,JUNIT 依赖项。使用Maven,类似这样的事情:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.2</version> </dependency>放置在 src/test/java 中的测试类:
@TransactionConfiguration(defaultRollback = true) @ContextConfiguration({ "classpath:test-spring-context.xml" }) @Transactional @RunWith(SpringJUnit4ClassRunner.class) public class SomeTests { // ... }引用放置在 src/test/resources 中的 DAO 数据源的 spring 设置的上下文文件。在您的 test-spring-context.xml 中的某处:
<import resource="datasource-test.xml" />
Now for example in eclipse you can run the project as a JUNIT test.
现在,例如在 eclipse 中,您可以将项目作为 JUNIT 测试运行。
Need more details? Is this solution applicable?
需要更多细节?这个解决方案适用吗?

![spring java.io.FileNotFoundException 的原因:类路径资源 [services.xml] 无法打开,因为它不存在](/res/img/loading.gif)