I'm reading a code that I didn't write. I stumbled upon the following statement:
context.checking(new org.jmock.Expectations() {
{
allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_NEWS);
will(returnValue(true));
allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_STAT);
will(returnValue(true));
allowing(habilitationManager).getUser();
will(returnValue(getUserMock()));
oneOf(parametreService).getParametre(PPP);
will(returnValue(getMockPPP()));
}
});
I understand that the methods called inside the second { ... }
are Expectations
methods.
{ ... }
?
It's an anonymous class, with an instance initializer block in it. So to separate the two out:
// This is an anonymous class
Expectations expectations = new Expectations() {
// Class body here
};
class Foo {
// This is an instance initializer block in a normal class
{
System.out.println("You'll see this via either constructor");
}
Foo(int x) {}
Foo(String y) {}
}
An instance initializer is implicitly called at the same time as instance variable initializers, in textual order, before the body of any constructor.
See more on this question at Stackoverflow