How does "static reflection" work in java? (e.g. in mockito or easymock)

I am a .NET guy, and I'm mostly C # code.

Starting with C # 3.0, we can use lambda expressions and expression trees to use static reflection . For example, you can implement GetMethodNamein the following snippet to return the name of the method passed in the parameter:

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

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

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

How it works?

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

+5
source share
4

. , , . (, RealProxy .NET, Proxy Java) .

EasyMock Proxy ( , ), : org.easymock.internal.JavaProxyFactory.

+5

Java mock- :

, ( ), " ". , ( , , ). , - . . .. , (, , ), .

, " ", mock ( + ).

+4

mockito easymock, , , . mockedList.get(0) - . get mockedList, when.

+2
source

mockedList.get(0)is the method invocation syntax and does just that. What this method does is not entirely clear. mockedListthe runtime type will be a subclass LinkedListreturned by a method mockthat can be implemented as if the fake structure were considered necessary.

+1
source

All Articles