In JUNIT tests, I'm using JMOCK. In some samples, I have seen code similar to the following snippet:

        mock.checking(new Expectations(){
            {
                allowing(tmp).assign(
                        with(any(String.class)),
                        with(any(String.class)),
                        with(any(Integer.class)),
                        with(any(Boolean.class)));
                will(returnValue("BLAH"));
            }
        });

I understand that tmp is a class mocked by JMOCK, and it will return "BLAH" from assign.

However, why are there two sets of curly brackets? Why does the new Expectations(){}? They're nested with no outside definition.

Why is this like this?

有帮助吗?

解决方案

That's called a technique referred to as double-brace initialization. The first set create an anonymous inner class, the second set perform instance initialization (as opposed to static initialization). This allows you, in this case, to create an Expectations object and do some set-up work inline.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top