gpt4 book ai didi

java - 方法调用同一个类中的其他方法: bad design?

转载 作者:行者123 更新时间:2023-12-02 00:03:17 25 4
gpt4 key购买 nike

EasyMock 3.0 文档 (http://www.easymock.org/EasyMock3_0_Documentation.html) 声称确实如此,但没有解释原因(强调我的):

Sometimes you may need to mock only some methods of a class and keep the normal behavior of others. This usually happens when you want to test a method that calls some others in the same class. So you want to keep the normal behavior of the tested method and mock the others.

In this case, the first thing to do is to consider a refactoring since most of the time this problem caused by a bad design.

那么,这是正确的吗?为什么?

最佳答案

问题不在于调用自己的方法,而在于模拟这些调用的必要性表明了另一个问题。否则,您可以只测试公共(public)方法并验证它们的行为。您不需要关心调用的其他方法,而只需关心可观察的行为。

关于java - 方法调用同一个类中的其他方法: bad design?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14407306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com