AuctionChannel { public void sendMessageTo(String message, Iterable<AuctionDescription> auctionDescriptions) { for (AuctionDescription auctionDescription : auctionDescriptions) { final AuctionConnection connection = connectionFinder.findConnectionFor(auctionDescription); if (connection != null) { connection.send(message); return; } } } AuctionChannel(AuctionConnectionFinder connectionFinder); void sendMessageTo(String message, Iterable<AuctionDescription> auctionDescriptions);  }
@Test public void sendsMessageToValidConnection() { context.checking(new Expectations() {{ allowing(connectionFinder).findConnectionFor(description); will(returnValue(connection)); oneOf(connection).send(MESSAGE_BODY); }}); auctionChannel.sendMessageTo(MESSAGE_BODY, asList(description)); }
@Test public void searchesConnectionsInOrder() { final Sequence connectionSearch = context.sequence("connection"); context.checking(new Expectations() {{ oneOf(connectionFinder).findConnectionFor(description0); inSequence(connectionSearch); will(returnValue(null)); oneOf(connectionFinder).findConnectionFor(description1); inSequence(connectionSearch); will(returnValue(null)); oneOf(connectionFinder).findConnectionFor(description); inSequence(connectionSearch); will(returnValue(connection)); oneOf(connection).send(MESSAGE_BODY); }}); auctionChannel.sendMessageTo(MESSAGE_BODY, asList(description0, description1, description)); }