|
This page describes main categories of methods in a mock
created by MockCreator along with their purpose and usage.
ZeroOrMore methods are configured once per test to return the same
value or to throw the same exception at each invocation.
There is no limit on order or number of calls to the method.
The method could even be not called during the test, and it
still considering alright.
| mock method(s) |
purpose |
example |
|
expectZeroOrMoreFoo().returns(retVal)
|
The mock is configured to return the same value at every method foo()
invocation. Any parameters that are passed to foo() are ignored
(unless more specific ZOMs
or expectations are configured).
Hint: useful to configure getters.
|
mock.expectZeroOrMoreGetSize()
.returns(10);
...
mock.getSize(); // returns 10
mock.getSize(); // again returns 10
mock.getSize(); // once again
mock.expectZeroOrMoreMove()
.returns(true);
...
mock.move(5,6); // true
mock.move(10,1); // true
...
|
|
expectZeroOrMoreFoo().throwable(ex)
|
The mock is configured to throw the same exception at every method foo()
invocation. There is no limit on order or number of calls to foo().
Useful to configure always throwing methods (like ones throwing
NotImplementedException).
Hint: by setting method to throw exception you can explicitely
tell reader that you expect foo() never be called in test.
|
mock.expectZeroOrMoreGetAccount()
.throwable(new NoRightsException());
...
mock.getAccount(); // throws!
mock.getAccount(); // again throws
mock.expectZeroOrMoreDeposit()
.throwable(new NoRightsException());
...
mock.deposit("john",1000); // throws
mock.deposit("jane",1,0.5); // throws
|
|
expectZeroOrMoreFoo(p1,p2,...).returns(retVal)
|
The method is identical to simple variant,
except it configures ZOM for given set of parameters only.
Any other set of parameters will throw MockException unless it matches
another ZOM or expectation.
|
mock.expectZeroOrMoreDeposit("john",100)
.returns(true);
mock.expectZeroOrMoreDeposit("jane",100)
.returns(false);
...
mock.deposit("john",100); // true
mock.deposit("jane",100); // false
mock.deposit("babe",200); // unexpected
|
|
expectZeroOrMoreFoo(p1,...).throwable(ex)
|
Identical to previous, except it throws an exception.
|
mock.expectZeroOrMoreDeposit("john",100)
.throwable(new NoRights());
mock.expectZeroOrMoreDeposit("jane",100)
.throwable(new Disabled());
...
mock.deposit("jane",100); // throws Disabled
mock.deposit("john",100); // throws NoRights
mock.deposit("babe",200); // unexpected call
|
|
acceptZeroOrMoreFoo(p1,...).returns(ret)
|
More flexible variant of ZOM.
Each parameter can accept not only exact parameters, but also
an object implementing ExpectableParameter interface. This allows
to ignore specific parameters, define parameters range, etc. Primitives
shall be wrapper into corresponding Object types (e.g. int -> Integer).
|
mock.acceptZeroOrMoreDeposit("jane",
new ExpectFloat(100.0,10.0)).
returns(true);
mock.acceptZeroOrMoreDeposit("jack",
new Integer(777)).
returns(true);
...
mock.deposit("jane",90.0); // true
mock.deposit("jane",110.0); // true
mock.deposit("jack",777); // true
|
|
acceptZeroOrMoreFoo(p1,...).throwable(th)
|
More flexible variant of ZOM.
Each parameter can accept not only exact parameters, but also
an object implementing ExpectableParameter interface. This allows
to ignore specific parameters, define parameters range, etc. Primitives
shall be wrapper into corresponding Object types (e.g. int -> Integer).
|
mock.acceptZeroOrMoreDeposit("john",new Ignore()).
throwable(new NoRights());
...
mock.deposit("john",33); // throws NoRights
|
|
Expected methods are the methods that are expected to happen
in a test in a given order. Any unexpected method results in
MockException (unless there are matching configured methods in
ZOMs.
| mock method(s) |
purpose |
example |
|
expectFoo(p1,...).returns(ret);
|
At this moment of the test we expect a call to foo(p1,...) and instruct it
to return value ret.
|
mock.expectSum(1,2)
.returns(3);
mock.expectSum(2,3)
.returns(5);
...
mock.sum(1,2); // 3
mock.sum(2,3); // 5
mock.sum(2,3); // unexpected call
|
|
expectFoo(p1,...).throwable(th);
|
At this moment of the test we expect a call to foo(p1,...) and instruct it
to throw exception th.
|
mock.expectLn(-1.0)
.throwable(new DomainError());
...
mock.ln(-1.0); // throws
mock.ln(1.0); // unexpected call
|
|
Acceptable expectations are variants of expectations,
but they are more flexible. Each and every parameter could be not only
an exact value, but also an object implementing ExpectableParameter
interface. The interface is similar to comparator in that it returns
true or false depending on does passed actual parameter match the
expectation or not. For example, built-in Ignore class always returns
true, effectively matching any passed actual parameter.
Primitive types must be wrapped into objects (int -> Integer,...).
| mock method(s) |
purpose |
example |
|
mock.acceptFoo(p1,...).returns(ret);
|
At this moment of the test we expect a call to foo(p1,...) and
instruct it to return value ret. Any of the paramters could be
ExpectableParameter implementation, such as Ignore, ExpectDate or
ExpectRegexp.
|
// do not care about the first parameter
mock.acceptSave(new Ignore(),new Integer(1)).returns(123);
...
mock.save(conn,1); // 123
mock.acceptSetUserName(new ExpectRegexp("[a-zA-Z0-9\_]{5,8}"));
...
mock.setUserName("john2"); // ok
|
|
mock.acceptFoo(p1,...).throwable(ex);
|
At this moment of the test we expect a call to foo(p1,...) and instruct it
to throw the given exception. Any of the paramters could be
ExpectableParameter implementation, such as Ignore or ExpectSame.
|
// for float in range 0.0-1.0 throw the exception
mock.acceptPercent(
new ExpectFloat(0.5,0.5)).
throwable(new DomainException());
...
mock.percent(0.99); // throws
|
|
Blocks specify expected calls with no predefined order. Every expected
call in a block must be called once and only once (duplicate expectations
are allowed though), and block cannot be left until all expectations are
called.
| mock method(s) |
purpose |
example |
MockCore.startBlock()
MockCore.endBlock()
|
Method startBlock() begins block body, endBlock() finishes it.
Subblocks are not allowed.
Usage of ZOMs
inside a block is allowed, but it does not differ from configuring it
outside the block.
|
// no block: error
mock.expectRight(5);
mock.expectUp(3);
...
mock.up(3); // unexpected call!
mock.right(5);
// in block: okey
MockCore.startBlock();
mock.expectRight(5);
mock.expectUp(3);
MockCore.endBlock();
...
mock.up(3);
mock.right(5);
// duplicate calls: okey
mock.startBlock();
mock.expectRight(5);
mock.expectUp(3);
mock.expectUp(3);
mock.endBlock();
...
mock.up(3);
mock.right(5);
mock.up(3);
|
|
MockCreator attempts to simplify used often tasks. To do that
utility methods are used. So far there is only one such method,
but a few others are considered.
| method(s) |
purpose |
example |
MockCore.setTimeTolerance(long millis)
|
The method make Date comparisions not exact,
but with some allowed deviation. For example,
expectSetDate(date) will require exactly the same
date, up to milliseconds, to match. After setTimeTolerance(1000)
the setDate() call can happily accept dates in range from a
second before the expected date to a second after.
This function is especially useful when testing classes that
add current timestamp to the call (e.g. loggers). Before we
had to use Ignore() to skip the date check, or use ExpectDate.
setTimeTolerance() though acts not only on a specific expectation,
but on a whole test.
The value is reset back to zero when MockCore.reset() is called,
i.e. the value should be set for each test separately.
|
MockCore.setTimeTolerance(1000);
mock.expectLog(new Date(),"oops");
logger.setLoggerImpl(mock);
logger.log("oops");
|
|

|