From: Steve F. <st...@m3...> - 2002-05-25 21:04:49
|
just a quickie before running off to XP2002... From: "Stephen or Carlene Peterson" <csp...@ya...> > I find applying Mock-Objects and writing my tests first to be > very difficult. Often when adding new code, it seems tortuously > slow, which suggests that I am just not 'getting' it. Usually > time pressure compells me to skip writing tests first, and this > frustrates me. I totally buy into the test-first-design > philosophy, but have trouble applying it. It can take a while when starting a new lump of code to build a critical mass of test infrastructure. The most useful part of test-first is its effect on design. How long can do you go before dropping out? > I have domain-code that receives objectA as a parameter. > It needs to call method isObjectNull on object A with a string > parameter "LockingTableName". The simple case is where a true > is returned, and the program exists. Another test is where a > false returns, and then a series of calls are made. > > I need to verify that calls are made, sometimes with certain > parameters and return values. How do I do this? > > The example code in the Endo-Testing example shows the > domain-code, the test-code, but does not show the code > implementing the mock objects. For example: > public void testUpdateEmployee() throws SQLException { MockObjectA mockA = new MockObjectA(); mockA.setupIsObjectNull(true); mockA.setExpectedTableName("LockingTableName"); obj.isObjectNull(mockA); mockA.verify(); } class MockObjectA implements Verifiable { private boolean isObjectNull; private ExpectationValue tableName = new ExpectationValue("table name"); public void setupIsObjectNull(boolean isNull) { isObjectNull = isNull; } public void setExpectedTableName(String name) { tableName.setExpected(name); } public boolean isObjectNull(String name) { tableName.setActual(name); } // if you inherit from MockObject, there's a verify() method that works using reflection. public void verify() { tableName.verify(); } } That said, I've been moving towards a lighter style for small mock objects, using inner classes with local variables. Still, this will get you started. S. |