简体   繁体   English

服务层测试:使用dbUnit时是否仍然是单元测试而不是模拟本机Spring域层?

[英]Service layer testing: Is it still unit testing when using dbUnit and not mocking a native Spring domain layer?

I have a service layer and a domain layer. 我有一个服务层和一个域层。 I use plain native Spring repositories for the domain layer and in my test setup I'm mocking the db with dbunit . 我使用普通的本机Spring存储库作为域层,在我的测试设置中,我用dbunit

@Repository
public interface ExampleRepository extends PagingAndSortingRepository<ExampleEntity, Long>, JpaSpecificationExecutor<ExampleEntity> {
}

Naturally I will assume that the Spring repository implementation has no errors and therefor the domain layer is no subject to tests. 当然,我将假设Spring存储库实现没有错误,因此域层不受测试。

My general knowledge about unit testing is that I would need to mock my domain layer when writing unit tests for my service layer. 我对单元测试的一般知识是,在为服务层编写单元测试时,我需要模拟我的域层。

With my assumption that the domain layer does not need to be tested, and the with the fact, that I use dbunit to mock my database, am I allowed to use this setup for unit tests of my service layer? 我假设域层不需要进行测试,事实上,我使用dbunit来模拟我的数据库,我可以使用这个设置来进行服务层的单元测试吗?

If I understood it correctly, in your tests the Spring context is boostraped, your service layers is tested along with your domain layer, and the data is inserted in your test database using DBUnit. 如果我理解正确,在您的测试中,Spring上下文会被提升,您的服务层将与您的域层一起进行测试,并使用DBUnit将数据插入您的测试数据库中。

With that, let's start: 有了它,让我们开始:

My general knowledge about unit testing is that I would need to mock my domain layer when writing unit tests for my service layer. 我对单元测试的一般知识是,在为服务层编写单元测试时,我需要模拟我的域层。

If you want to do a Unit Test , then this is correct. 如果你想进行单元测试 ,那么这是正确的。 The way you describe your tests, it depends on both layers to be correct, and the DB must be running so, per definition they are integration tests. 您描述测试的方式,它取决于两个层是否正确,并且DB必须运行,因此,根据定义,它们是集成测试。

With my assumption that the domain layer does not need to be tested, and the with the fact, that I use dbunit to mock my database, am I allowed to use this setup for unit tests of my service layer? 我假设域层不需要进行测试,事实上,我使用dbunit来模拟我的数据库,我可以使用这个设置来进行服务层的单元测试吗?

I wouldn't call the use of DBunit a database mock. 我不会将DBunit称为数据库模拟。 It surely helps populating the database, but the DB is still needed to run the tests, and your domain layer will be executed (JPA, Hibernate or whatever ORM framework will be generating SQLs that will be executed against the DB). 它肯定有助于填充数据库,但仍然需要DB来运行测试,并且将执行您的域层(JPA,Hibernate或任何ORM框架将生成将针对DB执行的SQL)。 If you really want to mock the domain layer and the DB then you should consider using mocks (many mock libraries can help with that). 如果你真的想要模拟域层和数据库,那么你应该考虑使用模拟(许多模拟库可以帮助你)。

As for the final question, well if this setup is working for your project I suppose it's OK, but I just wouldn't call it Unit Testing , but Integration Testing . 至于最后一个问题,如果这个设置适合你的项目,我想它没关系,但我不会称之为单元测试 ,而是集成测试 You'll have to accept that if your domain layer for some reason changes, it will probably break your service layers tests. 您必须接受,如果您的域层由于某种原因发生变化,它可能会破坏您的服务层测试。

There are pros and cons on both approaches (unit vs integration). 两种方法都有利弊(单位与整合)。 If it helps you grow your project with quality and good maintainability, then there is no "right" or "wrong" approach. 如果它有助于您以高质量和良好的可维护性来扩展您的项目,那么就没有“正确”或“错误”的方法。

No, testing with dbUnit is integration testing. 不,使用dbUnit进行测试是集成测试。

  • Unit testing involves only the small unit of code under test with mocking of any additional interactions. 单元测试仅涉及测试中的一小部分代码,模拟任何其他交互。
  • Integration testing involves a broader scope of the system with an external service, such as testing SQL with a database server and sending an email using email server. 集成测试涉及具有外部服务的更广泛的系统范围,例如使用数据库服务器测试SQL并使用电子邮件服务器发送电子邮件。 In both cases, we use "test doubles" instead of the real production ones to verify the system correctly interacted with the external service. 在这两种情况下,我们使用“测试双打”而不是真实的生产来验证系统是否与外部服务正确交互。

There are many test types, and the most common I see and implement are unit, integration, acceptance, and performance. 有许多测试类型,我看到和实现的最常见的是单元,集成,接受和性能。 All are valuable and have different pros/cons. 所有这些都是有价值的,有不同的优点/缺点 It's best to have all types for a quality system. 最好是所有类型的质量系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM