### Question:
ForEachModelAction implements Action { @Override public void execute(ActionContext context) { Object original = context.getVariable(targetVariable); try { for(Model model: context.getModels()) { if(selector.apply(model)) { try { Address address = model.getAddress(); context.logger().debug("[{}] to [{}] because it matched [{}]", delegate, address, selector); context.setVariable(targetVariable, address); delegate.execute(context); } catch(Exception e) { context.logger().warn("Error executing action [{}]", delegate.getDescription(), e); } } else { context.logger().trace("[{}] did not match [{}]", model, selector); } } } finally { context.setVariable(targetVariable, original); } } ForEachModelAction(Action delegate, Predicate<Model> selector, String targetVariable); @Override String getName(); @Override String getDescription(); @Override void execute(ActionContext context); @Override String toString();  }### Answer:
@Test public void testNoMatchingModels() { EasyMock.replay(mockAction); ForEachModelAction selector = new ForEachModelAction(mockAction, Predicates.<Model>alwaysFalse(), "address"); selector.execute(context); EasyMock.verify(mockAction); }
@Test public void testOneMatchingModel() { mockAction.execute(context); EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Object answer() throws Throwable { assertEquals(model1.getAddress(), context.getVariable(targetVariable)); return null; } }); EasyMock.replay(mockAction); ForEachModelAction selector = new ForEachModelAction(mockAction, Predicates.<Model>equalTo(model1), "address"); selector.execute(context); assertNull(context.getVariable(targetVariable)); EasyMock.verify(mockAction); }
@Test public void testMultipleMatchingModels() { mockAction.execute(context); EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Object answer() throws Throwable { assertEquals(model1.getAddress(), context.getVariable(targetVariable)); return null; } }); mockAction.execute(context); EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Object answer() throws Throwable { assertEquals(model2.getAddress(), context.getVariable(targetVariable)); return null; } }); mockAction.execute(context); EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() { @Override public Object answer() throws Throwable { assertEquals(model3.getAddress(), context.getVariable(targetVariable)); return null; } }); EasyMock.replay(mockAction); ForEachModelAction selector = new ForEachModelAction(mockAction, Predicates.<Model>alwaysTrue(), "address"); selector.execute(context); assertNull(context.getVariable(targetVariable)); EasyMock.verify(mockAction); }