gpt4 book ai didi

java - 在 junit 3 中模拟一个具体类

转载 作者:行者123 更新时间:2023-11-28 21:13:24 24 4
gpt4 key购买 nike

我有一个具有以下构造函数的类:

class Foo {
Foo (Bar bar) {
...
}
}

我正在尝试为此类编写单元测试并模拟对 Bar 的依赖。但是,我必须使用 JUnit 3 并且 Bar 是具体类型。有人有什么想法吗?我不能使用 EasyMock 类扩展(需要 JUnit 4)并且在 Mockito 上没有成功。我正在考虑的一种(特别难看的)解决方案如下:

interface IBarWrapper {
void barMethod();
}

class BarWrapper implements IBarWrapper {
void barMethod() {
bar.barMethod();
}
}

class Foo {
Foo (IBarWrapper wrapper) {
...
}
}

但我不喜欢更改我的实际代码以适应测试的想法。

最佳答案

你可以继承Bar。无需使事情过于复杂。

关于java - 在 junit 3 中模拟一个具体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310114/

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