public class MockWriter extends java.io.Writer implements Verifiable
Writer
.
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. }
Constructor and Description |
---|
MockWriter()
Instantiates a new mock writer which will act as a data sink.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Increments the close counter and asserts that this method was not
called too many times.
|
void |
flush()
This method will also throw an
IOException
if asked to do so by calling
setFlushShouldThrowException() . |
void |
setCloseShouldThrowException()
Sets the mock's behavior when closing.
|
void |
setExpectedCloseCalls(int calls)
Sets the expected number of times that the
close()
method will be called. |
void |
setExpectedFlushCalls(int calls)
Sets the expected number of times that the
flush()
method will be called. |
void |
setExpectedSegment(java.lang.String aString)
Sets the value of the expected string segment.
|
void |
setFlushShouldThrowException()
Sets the mock's behavior when flushing.
|
void |
setWriteShouldThrowException()
Sets the mock's behavior when writing.
|
void |
verify()
Throw an AssertionFailedException if any expectations have not been met.
|
void |
write(char[] cbuf,
int off,
int len)
Either throws an exception or asserts a string segment for equality.
|
public MockWriter()
public void setWriteShouldThrowException()
write(char[], int, int)
will throw an
IOException
.public void setFlushShouldThrowException()
flush()
will throw
an IOException
.public void setCloseShouldThrowException()
close()
will
throw an IOException
.public void setExpectedFlushCalls(int calls)
flush()
method will be called.flush()
public void setExpectedCloseCalls(int calls)
close()
method will be called.close()
public void setExpectedSegment(java.lang.String aString)
write(char[], int, int)
method
is called, a string is instantiated with the passed array and compared
to the aString
parameter of this method. If the two strings
differ, an AssertionFailedError
will be thrown.ExpectationSegment
,
write(char[], int, int)
public void write(char[] cbuf, int off, int len) throws java.io.IOException
write
in class java.io.Writer
java.io.IOException
ExpectationSegment
,
#setWriteShouldThrowException(boolean)
public void flush() throws java.io.IOException
IOException
if asked to do so by calling
setFlushShouldThrowException()
.
Please note that the call count will be incremented before the
check for the exception is done.flush
in interface java.io.Flushable
flush
in class java.io.Writer
java.io.IOException
setExpectedFlushCalls(int)
,
setFlushShouldThrowException()
public void close() throws java.io.IOException
IOException
if asked to do so by calling
setCloseShouldThrowException()
.
Please note that the call count will be incremented before the
check for the exception is done.close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.Writer
java.io.IOException
setExpectedCloseCalls(int)
,
setCloseShouldThrowException()
public void verify()
Verifiable
verify
in interface Verifiable
Copyright © 2002 Mock Objects. All Rights Reserved.