我设计了实现一些服务器协议的Java 类。例如,它有 getProtocolVersion() 方法,该方法返回最新的协议版本。该类的对象通过InputStream
和OutputStream
实现连接到服务器。
我对每个服务器命令的存根进行了测试。每个测试都会创建带有预期客户端请求和服务器响应的 ByteArrayInputStream 和 ByteArrayOutputStream ,以便我可以使用 JUnit 对协议实现执行逻辑单元测试。它可以工作,但是为每个命令测试创建夹具(准备数据)太无聊了。
Is it really to use mocking in my case? Is this solution less complex?如何通过输入和输出流模拟数据序列?
I've designed Java class that implements some server protocol. For example, it has getProtocolVersion()
method, which return latest protocol version. Object of this class is connected to the server through InputStream
and OutputStream
implementations.
I have a test with stubs for each server command. Every test creates ByteArrayInputStream
and ByteArrayOutputStream
with expected client requests and server responses so I can perform logic unit testing of my protocol implementation with JUnit. It's works but creating fixture (prepare data) for each command test is too boring.
Is it really to use mocking in my case? Is this solution less complex? How to mock data sequences through input and output streams?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将 Streams 包装在更容易创建和断言的对象中。这可以用于端到端测试。
对于单元测试,您可以将逻辑提取到可独立测试的方法中,而无需设置复杂的装置。
You could wrap the
Streams
in an object that is easier to create and assert with. This can serve for end-to-end tests.For unit tests, you can extract the logic into methods that are independently testable without having to setup a complex fixture.