使用 TDD:与客户端/服务器进行文件同步

发布于 2024-12-03 10:06:41 字数 626 浏览 1 评论 0原文

我开始尝试学习 tdd 并想在实际项目中使用它。所以我决定用java编写一个简单的客户端和服务器文件同步程序。 或多或少完成了客户端部分后,我在编写服务器部分时陷入了困境。

为了在不访问外部资源的情况下对逻辑进行单元测试,我将它们放在单独的类中,以便我可以模拟它们。到目前为止,一切都很好。

现在这是我的问题:

这张图片显示了我想象的一切的样子,其中绿色部分已完成黄色部分尚未实施。

就我而言,我必须向 RemoteServer 传递 ConnectionManagerConnectionManager 需要一个 FileAdapter 和一个 Communicator 才能创建 ClientConnectionHandler。这对我来说听起来有点过分了。这对于 tdd'ing 来说是正常的还是我为了保持一切可测试而做错了什么?

编辑:类 ClientConnectionHandler 只负责服务器端的文件同步逻辑,这意味着:遵循我自己的迷你协议从客户端接收文件。

I started trying to learn tdd and wanted to use it on a real project. So I decided on programming a simple file synchronization with a client and a server in java.
After more or less finishing the client part, I got stuck while writing the server part.

In order to unit test the logic without accessing external resources, I put those in separate classes, so that I could mock them. So far, so good.

Now here's my question:

This image shows how I imagined everything could look like, where green parts are finished and the yellow parts are not yet implemented.

In my case I'd have to pass RemoteServer a ConnectionManager. The ConnectionManager would a need a FileAdapter and a Communicator in order to create a ClientConnectionHandler. That sounds like a little bit too much passing for me. Is this normal for tdd'ing or am I doing something wrong in order to keep everything testable?

edit: The class ClientConnectionHandler is only responsible for the file sync logic on the server side, which means: Following my own mini protocol to receive files from a client.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-12-10 10:06:41

这很正常。如果您得到一个具有许多依赖项的类,那么它可能也有太多的责任。但是,一个类依赖于另一个类,而另一个类又依赖于另一个类——这是正常的。

这就是为什么 IoC 容器库如 Spring 很流行。它们使连接(配置和解析)所有依赖项变得更加容易。

It's pretty normal. If you get one class that has many dependencies, it likely has too many responsibilities as well. But having one class that depends on another class that depends on another - that's normal.

This is why IoC Container libraries like Spring are popular. They make it easier to wire up (configure and resolve) all of the dependencies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文