简体   繁体   English

弹簧单元测试:直接自动执行接口的实现?

[英]Spring Unit Testing: Autowire the Implementation of an Interface directly?

Our Spring webapp implements a lot of autowired Interfaces. 我们的Spring webapp实现了许多自动连接的接口。 Some of the methods in an implementation of an interface do a lot. 接口实现中的一些方法做了很多。 There is a public point of entry into the method itself and much of the logic is abstracted into lots of private methods. 公共点进入方法本身,并且大部分逻辑被抽象为许多私有方法。 Unit and Integration tests are pretty bulky as a result since there is a lot to test for. 因此,单元和集成测试非常笨重,因为需要测试很多。 In terms of writing integration tests I was considering a pattern of having the test Autowire the implemention rather than the interface, changing the scope of these private methods to public, but not exposing them to the owning interface. 在编写集成测试方面,我正在考虑使用测试Autowire 实现而不是接口的模式,将这些私有方法的范围更改为public,但不将它们暴露给拥有的接口。

This would allow for unit testing these subroutines, while keeping the contract clean of lots of methods that no one would use outside of the scope of the service itself. 这将允许单元测试这些子例程,同时保持合同清理许多方法,没有人会在服务范围之外使用。

Is this a known pattern? 这是一个已知的模式吗? What are the drawbacks to this approach? 这种方法的缺点是什么?

It's my understanding that it's actually more beneficial to test the service implementations rather than just the interface. 我的理解是,测试服务实现而不仅仅是接口实际上更有益。

One way to do it would be to use implementations as parameters in unit tests, this way you only care about the contract. 一种方法是在单元测试中使用实现作为参数,这样你只关心合同。

Since unit testing does not need to cover everything this should usually be enough. 由于单元测试不需要涵盖所有内容,因此通常应该足够。

However, if you also want to test stuff outside the contract I'm not sure a standard approach has been designated. 但是,如果您还想测试合同之外的东西,我不确定是否已指定标准方法。 But do go ahead with testing implementations. 但请继续测试实施。

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

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