如何重用单元测试进行集成测试
我的应用程序有一个自定义服务器组件。
我们有一些针对业务逻辑的 JUnit 测试用例,并使用 JMockit 来模拟 DB 层。
对于集成测试,我们最终需要检查很多类似的条件。唯一的区别是我需要序列化内容并将其发送到套接字,而不仅仅是进行调用。
有没有任何(简单的)方法来概括测试,以便我可以为这两个测试指定一次这些条件?
My application has a custom server component.
We have some JUnit test cases for the business logic with JMockit in place to mock the DB layer.
For integration testing, we end up having a lot of similar conditions to check. The only difference is that I need to serialize stuff and send to a socket instead of just making a call.
Is there any (simple) way to generalize the tests so that I can specify those conditions once for both tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用构造辅助对象的虚拟 FactoryMethod 将测试放入基类中。
您的单元测试和集成测试继承自该类。 FactoryMethod 的实现会创建一些模拟对象或一些真实对象。
You can put the tests into a baseclass with a virtual FactoryMethod that constructs helper-objects.
Your unittest und integrationtest inherits from that class. The implementation of the FactoryMethod creates either some mock or some real object.
您可以将规则添加到一个文件中,您可以从集成测试和单元测试中引用该文件。
您应该用某种领域特定语言编写它,该语言可以由非技术利益相关者阅读,但也可以解析,例如:
You could add the rules to a file which you can reference from the integration test and the unit tests.
You should write it in some kind of domain specific language which can be read by non-technical stakeholders but can also be parsed, e.g.:
您的单元和集成测试只是另一段代码,需要应用您的主流代码的标准概念。因此,编写可重用代码、使用设计模式、对接口进行编码以及最终持续重构之类的事情也适用于测试。
以下SO线程有更多建议:
希望有帮助。
Your unit and integration tests are just another piece of code which would need to be applied the statndard concepts you have for your main stream code. So, things like writing reusable code, using design patterns, coding to interfaces and finally continuous refactoring would hold true for tests as well.
Following SO threads have more advice:
Hope that helps.