Analysis by Contract
Analysis by Contract is a term coined by Richard Mitchell and Jim McKim in their book
Design by Contract by Example.
package timeofday; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TimeOfDayTest { private TimeOfDaySpec classUnderTest; private final int HOUR = 12; private final int MINUTE = 27; private final int SECOND = 54; @Before public void setUp() { classUnderTest = new TimeOfDay(HOUR, MINUTE, SECOND); } @Test public void getHourTest() { assertEquals(HOUR, classUnderTest.getHour()); } @Test public void getMinuteTest() { assertEquals(MINUTE, classUnderTest.getMinute()); } @Test public void getSecondTest() { assertEquals(SECOND, classUnderTest.getSecond()); } @Test public void setHourTest() { classUnderTest.setHour(HOUR + 1); assertEquals(HOUR + 1, classUnderTest.getHour()); } @Test public void setMinuteTest() { classUnderTest.setMinute(MINUTE + 1); assertEquals(MINUTE + 1, classUnderTest.getMinute()); } @Test public void setSecondTest() { classUnderTest.setSecond(SECOND + 1); assertEquals(SECOND + 1, classUnderTest.getSecond()); } @Test public void isBeforeHourTest() { TimeOfDaySpec other = new TimeOfDay(HOUR + 1, MINUTE, SECOND); assertTrue(classUnderTest.isBefore(other)); assertFalse(other.isBefore(classUnderTest)); } @Test public void isBeforeMinuteTest() { TimeOfDaySpec other = new TimeOfDay(HOUR, MINUTE + 1, SECOND); assertTrue(classUnderTest.isBefore(other)); assertFalse(other.isBefore(classUnderTest)); } @Test public void isBeforeSecondTest() { TimeOfDaySpec other = new TimeOfDay(HOUR, MINUTE, SECOND + 1); assertTrue(classUnderTest.isBefore(other)); assertFalse(other.isBefore(classUnderTest)); } @Test public void isBeforeSameTest() { TimeOfDaySpec other = classUnderTest; assertFalse(classUnderTest.isBefore(other)); assertFalse(other.isBefore(classUnderTest)); } @Test public void isBeforeEqualsTest() { TimeOfDaySpec other = new TimeOfDay(HOUR, MINUTE, SECOND); classUnderTest.isBefore(other); assertFalse(classUnderTest.isBefore(other)); assertFalse(other.isBefore(classUnderTest)); } }
This test case is written test per test.
And test per test the addressed interface TimeOfDaySpec
- the interface under test - is growing.
Compiling the test case is possible by using the support of modern Java IDEs
to create an empty class TimeOfDay - the class under test -
which implements the interface under test TimeOfDaySpec
and is auto-completed each time the interface grows.
package timeofday; import de.vksi.c4j.Pure; public interface TimeOfDaySpec { void setHour(int hour); void setMinute(int minute); void setSecond(int second); @Pure int getHour(); @Pure int getMinute(); @Pure int getSecond(); @Pure boolean isBefore(TimeOfDaySpec other); } Please note that command query separation has already been accomplished to this interface, following the six principles. Methods annotated as pure are queries, all other methods are commands. Furthermore the queries have been separated into basic queries and derived queries, which is important for the next activity, Design by Contract. |