Package com.mockobjects.io
Class MockWriter
java.lang.Object
java.io.Writer
com.mockobjects.io.MockWriter
- All Implemented Interfaces:
Verifiable,Closeable,Flushable,Appendable,AutoCloseable
A mock
Writer.
Example usage
You may use the MockWriter like this:
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.
}
- Author:
- Francois Beausoleil, fbos@users.sourceforge.net
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Increments the close counter and asserts that this method was not called too many times.voidflush()This method will also throw anIOExceptionif asked to do so by callingsetFlushShouldThrowException().voidSets the mock's behavior when closing.voidsetExpectedCloseCalls(int calls) Sets the expected number of times that theclose()method will be called.voidsetExpectedFlushCalls(int calls) Sets the expected number of times that theflush()method will be called.voidsetExpectedSegment(String aString) Sets the value of the expected string segment.voidSets the mock's behavior when flushing.voidSets the mock's behavior when writing.voidverify()Throw an AssertionFailedException if any expectations have not been met.voidwrite(char[] cbuf, int off, int len) Either throws an exception or asserts a string segment for equality.
-
Constructor Details
-
MockWriter
public MockWriter()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.
-
-
Method Details
-
setWriteShouldThrowException
public void setWriteShouldThrowException()Sets the mock's behavior when writing. If this method has been called, thenwrite(char[], int, int)will throw anIOException. -
setFlushShouldThrowException
public void setFlushShouldThrowException()Sets the mock's behavior when flushing. If this method has been called, thenflush()will throw anIOException. -
setCloseShouldThrowException
public void setCloseShouldThrowException()Sets the mock's behavior when closing. If this method has been called, thenclose()will throw anIOException. -
setExpectedFlushCalls
public void setExpectedFlushCalls(int calls) Sets the expected number of times that theflush()method will be called.- See Also:
-
setExpectedCloseCalls
public void setExpectedCloseCalls(int calls) Sets the expected number of times that theclose()method will be called.- See Also:
-
setExpectedSegment
Sets the value of the expected string segment. When thewrite(char[], int, int)method is called, a string is instantiated with the passed array and compared to theaStringparameter of this method. If the two strings differ, anAssertionFailedErrorwill be thrown.- See Also:
-
write
Either throws an exception or asserts a string segment for equality.- Specified by:
writein classWriter- Throws:
IOException- See Also:
-
flush
This method will also throw anIOExceptionif asked to do so by callingsetFlushShouldThrowException(). Please note that the call count will be incremented before the check for the exception is done.- Specified by:
flushin interfaceFlushable- Specified by:
flushin classWriter- Throws:
IOException- See Also:
-
close
Increments the close counter and asserts that this method was not called too many times. This method will also throw anIOExceptionif asked to do so by callingsetCloseShouldThrowException(). Please note that the call count will be incremented before the check for the exception is done.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classWriter- Throws:
IOException- See Also:
-
verify
public void verify()Description copied from interface:VerifiableThrow an AssertionFailedException if any expectations have not been met.- Specified by:
verifyin interfaceVerifiable
-