具有特征的模拟类
是否有任何库提供用于模拟具有特征的类的工具(两者都可以是有状态的)?
简化示例:
trait T {
var xx: List[Int] = List[Int]()
def t(x: Int) {
xx ::= x //throws NPE, xx == null, even after implicit initialization
}
}
class A extends T {
}
class Testable(a: A) {
def bar() {
a.t(2)
}
}
@Test def testFoo() {
val a: A = mock[A]
val testable = new Testable(a)
testable.bar()
verify(a).t(2)
}
Is there any library that provides tools for mocking classes with traits (both can be statefull)?
Simplified example:
trait T {
var xx: List[Int] = List[Int]()
def t(x: Int) {
xx ::= x //throws NPE, xx == null, even after implicit initialization
}
}
class A extends T {
}
class Testable(a: A) {
def bar() {
a.t(2)
}
}
@Test def testFoo() {
val a: A = mock[A]
val testable = new Testable(a)
testable.bar()
verify(a).t(2)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Paul Butcher 一直在研究 Borachio,一个 Scala 模拟库。它支持对特征、类、函数和对象的模拟。有关详细信息,请参阅以下博客:
http://www.paulbutcher.com/2011/02/announcing-borachio-native-scala-mocking/" rel="nofollow"> paulbutcher.com/2011/02/announcing-borachio-native-scala-mocking/
http://www.paulbutcher.com/2011/ 07/power-mocking-in-scala-with-borachio/
Paul Butcher has been working on Borachio, a Scala mocking library. It supports mocking of traits, classes, functions and objects. See the following blogs for more information:
http://www.paulbutcher.com/2011/02/announcing-borachio-native-scala-mocking/
http://www.paulbutcher.com/2011/07/power-mocking-in-scala-with-borachio/
嗯......我没有答案,但我想我可以提供一个提示来说明问题的根源。我看了一下 A.class 并发现了这个(de.schauderhaft.testen 是我使用的包):
我不是字节码专家,但这
看起来对 t(Int) 的调用实际上是对静态方法的调用并且你不能模拟静态方法。 PowerMock 会有所帮助,但使用起来可能很难看。
Well ... I don't have an answer, but I think I can offer a hint at where the problem is coming from. I took a look at A.class and found this (de.schauderhaft.testen is the package I used):
I'm no byte code expert but this
looks like the call to t(Int) is actually a called to a static method and you can't mock static methods. PowerMock would help, but probably ugly to use.
我刚刚发布了 ScalaMock 2.0。除了函数和接口之外,ScalaMock 还可以模拟:
I just released ScalaMock 2.0. As well as functions and interfaces, ScalaMock can mock: