문제

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