From: Francois B. <fbe...@ft...> - 2002-08-22 02:08:13
|
Hi, I feel quite the fool now... Seems I should write test cases for mocks too. Anyway, the previously submitted MockWriter will throw a StackOverflowError when verifying. Below is code that really works. Sorry again for that. Have a nice day ! Francois Beausoleil ==================================================================package com.mockobjects.io; import com.mockobjects.ExpectationCounter; import com.mockobjects.ExpectationSegment; import com.mockobjects.Verifiable; import com.mockobjects.util.Verifier; import java.io.IOException; import java.io.Writer; /** * A mock {@link java.io.Writer Writer}. * <h3>Example usage</h3> * <p> * You may use the <code>MockWriter</code> like this: * <pre> * public void testSomething() throws IOException { * MockWriter out = new MockWriter(); * out.setExpectedSegment("some string"); * out.setExpectedFlushCalls(1); * out.setExpectedCloseCalls(1); * * ObjectUnderTest testee = new ObjectUnderTest(out); * out.verify(); * * // If we get here, the mock's flush() and close() methods were * // called exactly once each (order cannot be determined) and * // the write() method was called with the string "some string" in it. * }</pre> * </p> * @author Francois Beausoleil, fb...@us... */ public class MockWriter extends Writer implements Verifiable { private ExpectationSegment mySegment = new ExpectationSegment("String segment"); private ExpectationCounter myFlushCalls = new ExpectationCounter("flush calls"); private ExpectationCounter myCloseCalls = new ExpectationCounter("close calls"); private boolean myShouldWriteThrowException = false; /** * Instantiates a new mock writer which will act as a data sink. * Once instantiated, mocks of this class do not expect anything special. * Once the object is instantiated, you should set your expectations using * the provided methods. */ public MockWriter() { } /** * Sets the mocks behavior when writing. * When the {@link #write(char[],int,int) write(char[], int, int)} method * is called, if this method was called with <code>true</code>, the mock * will throw an {@link java.io.IOException IOException} instead of * asserting a string segment. * @param state Set to <code>true</code> if you want the mock to throw an * exception, <code>false</code> otherwise. */ public void setWriteShouldThrowException(boolean state) { myShouldWriteThrowException = state; } /** * Sets the expected number of times that the {@link #flush() flush()} * method will be called. * @see #flush() */ public void setExpectedFlushCalls(int calls) { myFlushCalls.setExpected(calls); } /** * Sets the expected number of times that the {@link #close() close()} * method will be called. * @see #close() */ public void setExpectedCloseCalls(int calls) { myCloseCalls.setExpected(calls); } /** * Sets the value of the expected string segment. * When the {@link #write(char[], int, int) write(char[], int, int)} method * is called, a string is instantiated with the passed array and compared * to the <code>aString</code> parameter of this method. If the two strings * differ, an {@link junit.framework.AssertionFailedError} will be thrown. * @see com.mockobjects.ExpectationSegment * @see #write(char[], int, int) */ public void setExpectedSegment(String aString) { mySegment.setExpected(aString); } /** * Either throws an exception or asserts a string segment for equality. * @see com.mockobjects.ExpectationSegment * @see #setWriteShouldThrowException(boolean) */ public void write(char cbuf[], int off, int len) throws IOException { if (myShouldWriteThrowException) { throw new IOException("Exception, as requested"); } mySegment.setActual(new String(cbuf, off, len)); } /** * Increments the flush counter and asserts that this method was not * called too many times. * @see #setExpectedFlushCalls(int) */ public void flush() throws IOException { myFlushCalls.inc(); } /** * Increments the close counter and asserts that this method was not * called too many times. * @see #setExpectedCloseCalls(int) */ public void close() { myCloseCalls.inc(); } public void verify() { mySegment.verify(); myFlushCalls.verify(); myCloseCalls.verify(); } } ================================================================== |
From: Steve F. <st...@m3...> - 2002-08-22 09:54:38
|
I've checked this in twice. 1) more or less as you sent it. 2) invoking committer's privilege I've: - changed setWriteShouldThrowException() not to take a parameter. The idea is that new instances should be constructed for each test, so you shouldn't be toggling this value on and off. It's either the default (off) or set. - changed some variable names - changed verify() to use the Verifier helper class. If you don't like this let me know and we can rollback, in the meantime, both versions are in CVS. Steve ----- Original Message ----- From: "Francois Beausoleil" <fbe...@ft...> To: <moc...@li...> Sent: Thursday, August 22, 2002 3:08 AM Subject: [MO-java-dev] com.mockobjects.io.MockWriter correction > [...] |
From: Tim M. <tim...@po...> - 2002-08-22 23:38:25
|
I disagree with that change Steve. I know you prefer it - but its much clearer to have a boolean parameter. It also allows for a clearer test when you explicitly use false, to communicate that you really don't want an exception. It also lets you set true in a test setup method and for an odd test set it back to false. -----Original Message----- From: moc...@li... [mailto:moc...@li...]On Behalf Of Steve Freeman Sent: 22 August 2002 10:53 To: moc...@li...; mockobjects users Subject: Re: [MO-java-dev] com.mockobjects.io.MockWriter correction I've checked this in twice. 1) more or less as you sent it. 2) invoking committer's privilege I've: - changed setWriteShouldThrowException() not to take a parameter. The idea is that new instances should be constructed for each test, so you shouldn't be toggling this value on and off. It's either the default (off) or set. - changed some variable names - changed verify() to use the Verifier helper class. If you don't like this let me know and we can rollback, in the meantime, both versions are in CVS. Steve ----- Original Message ----- From: "Francois Beausoleil" <fbe...@ft...> To: <moc...@li...> Sent: Thursday, August 22, 2002 3:08 AM Subject: [MO-java-dev] com.mockobjects.io.MockWriter correction > [...] ------------------------------------------------------- This sf.net email is sponsored by: OSDN - Tired of that same old cell phone? Get a new here for FREE! https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390 _______________________________________________ Mockobjects-java-dev mailing list Moc...@li... https://lists.sourceforge.net/lists/listinfo/mockobjects-java-dev --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.381 / Virus Database: 214 - Release Date: 02/08/2002 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.381 / Virus Database: 214 - Release Date: 02/08/2002 |