“静态反射”如何实现? 在java中工作? (例如,在mockito或easymock中)

发布于 2024-07-14 07:57:16 字数 761 浏览 4 评论 0原文

我是一个 .NET 人 - 我主要用 C# 编写代码。

从 C# 3.0 开始,我们可以利用 lambda 表达式和表达式树来使用 静态反射。 例如,可以在以下代码片段中实现 GetMethodName 来返回参数中传递的方法的名称:

string methodName = GetMethodName( o => o.DoSomething());
Console.WriteLine(methodName); // displays "DoSomething"

现在,当我查看 Java 世界中的 Mockito 示例(或 EasyMock 示例)时,我发现请参阅:

LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first");

它是如何工作的?

when 方法如何工作? 它如何将 mockedList.get(0) 解释为 对 get 方法的调用,并将 0 作为参数传递,而不是作为值?

I'm a .NET guy - and I mainly code in C#.

Since C# 3.0, we can leverage lambda expressions and expression trees to use static reflection. For example, it is possible to implement GetMethodName in the following snippet to return the name of the method passed in parameter:

string methodName = GetMethodName( o => o.DoSomething());
Console.WriteLine(methodName); // displays "DoSomething"

Now, when I look at Mockito samples (or EasyMock ones) in the java world, I see:

LinkedList mockedList = mock(LinkedList.class);
when(mockedList.get(0)).thenReturn("first");

How does it work?

How does the when method work ? How does it interpret mockedList.get(0) as a call to the get method with 0 passed as parameter and not as a value?

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

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

发布评论

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

评论(4

感性 2024-07-21 07:57:16

模拟库通常不适用于表达式树。 他们构建一个类型,该类型实现适当的接口并通过记录方法调用或验证方法调用并返回预编程的响应来响应方法调用。 这通常通过代理完成(例如 RealProxy 在 .NET 中,代理 in Java)或使用动态代码生成。

对于 EasyMock,它使用 Proxy(无论如何,对于接口),正如您在源代码中看到的那样:查看 org.easymock.internal.JavaProxyFactory

Mocking libraries don't typically work with expression trees. They build a type which implements the appropriate interface and responds to method calls either by recording them or validating them and returning the preprogrammed responses. This is usually done with either a proxy (e.g. RealProxy in .NET, Proxy in Java) or with dynamic code generation.

In the case of EasyMock, it uses Proxy (for interfaces, anyway), as you can see in the source code: look at org.easymock.internal.JavaProxyFactory.

尴尬癌患者 2024-07-21 07:57:16

Java模拟库通常是这样工作的:

当您创建模拟时,会创建一个实际的代理(无论是来自接口还是子类),实例处于“记录模式”。 这意味着任何后续调用都会被记录(方法名称、参数、预期返回)。 请注意,录音模式下的代理实际上除了记录通话之外什么也不做。 本身不涉及反射。 没有元数据发现等。当然,这些库做了一些技巧(例如将调用存储在线程局部变量中以处理返回 void 的方法),但想法保持不变。

然后,当“重播模式”启动时,模拟实例只需检查调用列表(方法+参数和返回值)中的期望。

Java mock libraries usually work like this:

When you create a mock, an actual proxy is created (be it from an interface or a sub-class), the instance is in "recording mode". This means that any subsequent call is recorded (method name, parameters, return expected). Notice that the proxy in recording mode does actually nothing but record the calls. There is no reflection per-se involved. No metadata discovery, etc. Of course these libraries do some tricks (such as storing invocations in a thread-local variable to handle methods that return void) but the idea remains the same.

Then, when the "replay mode" is started, the mock instance simply checks the expectations from the list of invocations (method+parameters & return values).

玩物 2024-07-21 07:57:16

我从未使用过mockito 或easymock,但我不认为该调用会执行您认为的操作。 它不会以任何特殊方式解释mockedList.get(0)get 方法通常在 mockedList 对象上执行,并将其结果交给 when

I’ve never worked with mockito or easymock but I don’t think the call does what you think it does. It does not interpret mockedList.get(0) in any special way. The method get is executed on the mockedList object normally, and the result of that is handed in to when.

永不分离 2024-07-21 07:57:16

mockedList.get(0) 是方法调用的语法,并且正是这样做的。 该方法的作用尚不清楚。 mockedList 的运行时类型将是 mock 方法返回的 LinkedList 的子类,可以按照模拟框架认为合适的方式实现。

mockedList.get(0) is the syntax for a method call, and does exactly that. What that method does is not exactly clear. mockedList's runtime type will be a subclass of LinkedList returned by the mock method, which can be implemented how ever the mocking framework sees fit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文