質問

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