具有特征的模拟类

发布于 2024-12-07 09:07:23 字数 417 浏览 1 评论 0原文

是否有任何库提供用于模拟具有特征的类的工具(两者都可以是有状态的)?

简化示例:

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 技术交流群。

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

发布评论

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

评论(3

画离情绘悲伤 2024-12-14 09:07:23

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/

幽蝶幻影 2024-12-14 09:07:23

嗯......我没有答案,但我想我可以提供一个提示来说明问题的根源。我看了一下 A.class 并发现了这个(de.schauderhaft.testen 是我使用的包):

  // Method descriptor #21 (I)V
  // Stack: 2, Locals: 2
  public bridge void t(int x);
    0  aload_0 [this]
    1  iload_1 [x]
    2  invokestatic de.schauderhaft.testen.T$class.t(de.schauderhaft.testen.T, int) : void [26]
    5  return
      Line numbers:
        [pc: 0, line: 13]
      Local variable table:
        [pc: 0, pc: 6] local: this index: 0 type: de.schauderhaft.testen.A
        [pc: 0, pc: 6] local: x index: 1 type: int 

我不是字节码专家,但这

2  invokestatic de.schauderhaft.testen.T$class.t(de.schauderhaft.testen.T, int) : void [26]

看起来对 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):

  // Method descriptor #21 (I)V
  // Stack: 2, Locals: 2
  public bridge void t(int x);
    0  aload_0 [this]
    1  iload_1 [x]
    2  invokestatic de.schauderhaft.testen.T$class.t(de.schauderhaft.testen.T, int) : void [26]
    5  return
      Line numbers:
        [pc: 0, line: 13]
      Local variable table:
        [pc: 0, pc: 6] local: this index: 0 type: de.schauderhaft.testen.A
        [pc: 0, pc: 6] local: x index: 1 type: int 

I'm no byte code expert but this

2  invokestatic de.schauderhaft.testen.T$class.t(de.schauderhaft.testen.T, int) : void [26]

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.

嘦怹 2024-12-14 09:07:23

我刚刚发布了 ScalaMock 2.0。除了函数和接口之外,ScalaMock 还可以模拟:

  • 单例和伴生对象(静态方法)
  • 对象创建(构造函数调用)
  • 具有私有构造函数的类
  • 最终类和具有最终方法的类
  • 运算符(具有符号名称的方法)
  • 重载方法

I just released ScalaMock 2.0. As well as functions and interfaces, ScalaMock can mock:

  • Classes
  • Singleton and companion objects (static methods)
  • Object creation (constructor invocation)
  • Classes with private constructors
  • Final classes and classes with final methods
  • Operators (methods with symbolic names)
  • Overloaded methods
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文