Java Reference
In-Depth Information
specification
.
specifyBehaviour
(
expect
);
Runner
.
current
.
recordSuccess
(
suite
,
description
);
}
catch
catch
(
AssertionError cause
) {
Runner
.
current
.
recordFailure
(
suite
,
description
,
cause
);
}
catch
catch
(
Throwable cause
) {
Runner
.
current
.
recordError
(
suite
,
description
,
cause
);
}
}
When our specifications want to describe an actual expectation, they use the
expect.that
clause. This means that our
Expect
class needs to have a method called
that
for users to
call, shown in
Example 8-33
.
This wraps up the object that gets passed in and can then ex-
pose fluent methods such as
isEqualTo
that throw the appropriate assertions if there's a spe-
cification failure.
Example 8-33. The start of the fluent expect chain
public
public final
final class
class
Expect
Expect
{
public
public
BoundExpectation
that
(
Object value
) {
return
return new
new
BoundExpectation
(
value
);
}
// Rest of class omitted
You may have noticed one more detail that I've so far ignored and that has nothing to do
with lambda expressions. Our
StackSpec
class didn't have any methods directly implemen-
ted on it, and I wrote the code inside. I've been a bit sneaky here and used double braces at
the beginning and end of the class definition:
public
public class
class
StackSpec
StackSpec
{{
...
}}
These start an anonymous constructor that lets us execute an arbitrary block of Java code, so
it's really just like writing out the constructor in full, but with a bit less boilerplate. I could
have written the following instead:
public
public class
class
StackSpec
StackSpec
{
public
public
StackSpec
() {
...