2011年12月20日 星期二

[Java] Double Brace Initialization

The first brace creates an anonymous inner class, and the second brace creates an initializer block that is executed when the anonymous class is created.

Example:
import org.jmock.Mockery;
import org.jmock.Expectations;

class PublisherTest extends TestCase {
    Mockery context = new Mockery();

    public void testOneSubscriberReceivesAMessage() {
        // set up
        final Subscriber subscriber = context.mock(Subscriber.class);

        Publisher publisher = new Publisher();
        publisher.add(subscriber);
        
        final String message = "message";
        
        // expectations
        context.checking(new Expectations() {{
            oneOf (subscriber).receive(message);
        }});

        // execute
        publisher.publish(message);
        
        // verify
        context.assertIsSatisfied();
    }
}
Please check new Expectations() {{ oneOf (subscriber).receive(message); }}

沒有留言:

張貼留言