Groovy:存根类型引用

发布于 2024-07-23 10:40:05 字数 668 浏览 6 评论 0原文

我有一个类似于

class MyClass {

  Foo foo
}

在某些情况下我不想初始化 foo 并希望消除对它的所有调用的 Groovy 类。 任何返回值的方法都不应该执行任何操作。 我可以这样做:

Foo.metaClass.method1 = {param -> }
Foo.metaClass.method2 = { -> }
Foo.metaClass.method3 = {param1, param2 -> }

虽然这可行,但它有几个问题

  1. 乏味且冗长,特别是如果 Foo 有很多方法
  2. 这将阻止对 Foo 的任何实例(不仅仅是 foo)的调用

尽管 Groovy 提供了一个 StubFor 类,如果我这样做:

this.foo = new groovy.mock.interceptor.StubFor(Foo)

我在运行时得到一个 ClassCastException 。 虽然如果我可以将 foo 重新定义为:

def foo

但由于我不会在这里讨论的原因,我不能这样做。

谢谢, 大学教师

I have a Groovy class similar to

class MyClass {

  Foo foo
}

Under certain circumstances I don't want to initialize foo and want to stub out all the calls to it. Any methods that return a value should do nothing. I could do it like this:

Foo.metaClass.method1 = {param -> }
Foo.metaClass.method2 = { -> }
Foo.metaClass.method3 = {param1, param2 -> }

While this will work, it has a couple of problems

  1. Tedious and long-winded, particularly if Foo has a lot of methods
  2. This will stub out calls to any instance of Foo (not just foo)

Although Groovy provides a StubFor class, if I do this:

this.foo = new groovy.mock.interceptor.StubFor(Foo)

I get a ClassCastException at runtime. Although this would work if I could redefine foo as:

def foo

But for reasons I won't go into here, I can't do that.

Thanks,
Don

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

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

发布评论

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

评论(4

夜空下最亮的亮点 2024-07-30 10:40:05

我找到了解决方案:

this.foo = {Object[] args -> println "I don't do anything"} as Foo

I found the solution:

this.foo = {Object[] args -> println "I don't do anything"} as Foo
深白境迁sunset 2024-07-30 10:40:05

您需要将 Foo 实例传递给 MyClass 对象。 在测试中,传递存根实现。 在真实的程序中传递真实的实现。

您不需要任何特殊的框架来编写存根。 如果您只是对 Foo 进行存根查询,并且对它的期望命令不感兴趣,那么手动编写存根实现会更容易。 模拟对象库是多余的,并且会让后来期望测试包含期望的代码读者感到困惑。

You need to pass the Foo instance to your MyClass object. In the test, pass in a stub implementation. In the real program pass in a real implementation.

You don't need any special framework to write the stub. If you're only stubbing queries to Foo and are not interested in expecting commands to it, then it's easier to write a stub implementation by hand. A mock object library is overkill and will confuse later readers of the code who will expect the test to include expectations.

相思故 2024-07-30 10:40:05

您也可以使用映射创建存根,从而可以存根多个方法,如下所示:

def stubbedList = [empty : { false }, get: {index -> "always return this"}] as ArrayList

如果您存根一个类(如上面的 ArrayList 示例),那么您不重写的方法将保持原样并在课堂上实施。 您还可以明显地对接口进行存根。

如果您需要模拟功能(即验证测试中的行为,例如计算对给定方法的调用次数),我强烈建议您查看 Spock测试框架。 它对更高级的存根和模拟提供了非常好的支持。

You could make a stub with a map as well, making it possible to stub multiple methods, like so:

def stubbedList = [empty : { false }, get: {index -> "always return this"}] as ArrayList

If you stub out a class (like in the ArrayList example above), then the methods that you do not override are kept as they are implemented in the class. You can also stub out interfaces obviously.

If you are in need of mocking capabilities (i.e verifying behaviour in a test, like counting the number of calls to a given method), I would strongly recommend checking out the Spock test framework. It has really great support for more advanced stubbing and mocking.

江湖彼岸 2024-07-30 10:40:05

我猜这只是为了测试目的。

该行:

Foo foo

创建一个 getter / setter 对,因此您可以通过执行以下操作在测试用例中注入模拟:

new MyClass().foo = new MockFoo()

如果您不想自己创建模拟,请尝试模拟库。 我建议使用 mockito。 有了这个小库,你可以做这样的事情:

import static org.mockito.Mockito.*;
new MyClass().foo = mock(Foo.class);

I'm guessing this is for test purposes only.

The line:

Foo foo

creates a getter / setter pair, so you can inject a mock in your test cases by doing this:

new MyClass().foo = new MockFoo()

If you don't want to create the mock for yourself, try a mocking library. I recommend using mockito. With this small library you can do something like this:

import static org.mockito.Mockito.*;
new MyClass().foo = mock(Foo.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文