Mammoth { public void timePasses() { if (state.getClass().equals(PeacefulState.class)) { changeStateTo(new AngryState(this)); } else { changeStateTo(new PeacefulState(this)); } } Mammoth(); void timePasses(); @Override String toString(); void observe();  }
@Test public void testTimePasses() { final Mammoth mammoth = new Mammoth(); mammoth.observe(); assertEquals("The mammoth is calm and peaceful.", appender.getLastMessage()); assertEquals(1 , appender.getLogSize()); mammoth.timePasses(); assertEquals("The mammoth gets angry!", appender.getLastMessage()); assertEquals(2 , appender.getLogSize()); mammoth.observe(); assertEquals("The mammoth is furious!", appender.getLastMessage()); assertEquals(3 , appender.getLogSize()); mammoth.timePasses(); assertEquals("The mammoth calms down.", appender.getLastMessage()); assertEquals(4 , appender.getLogSize()); mammoth.observe(); assertEquals("The mammoth is calm and peaceful.", appender.getLastMessage()); assertEquals(5 , appender.getLogSize()); }