Java Reference
In-Depth Information
public
public static
void
describe
(
String name
,
Suite behavior
) {
Description description
=
new
static
void
new
Description
(
name
);
behavior
.
specifySuite
(
description
);
}
Each suite has its code description implemented by the user using a lambda expression. This
of specifications. You'll notice it also takes a
Description
object as an argument, which we
passed into it from the
describe
method.
Example 8-30. Each suite of tests is a lambda expression implementing this interface
public
public interface
interface
Suite
Suite
{
public
public
void
void
specifySuite
(
Description description
);
}
Not only are suites represented by lambda expressions in our DSL, but so are individual spe-
cifications. They also need a functional interface, which I'll call
Specification
class, which I'll describe later.
Example 8-31. Each specification is a lambda expression implementing this interface
public
public interface
interface
Specification
Specification
{
public
public
void
void
specifyBehaviour
(
Expect expect
);
}
The
Description
instance we've been passing around comes in handy at this point. We
want our users to be able to fluently name their specifications with the
it.should
clause.
where the real work gets done, as this is the method that actually executes the lambda expres-
sion by calling its
specifySuite
method. Specifications will tell us they have failed by
throwing the standard Java
AssertionError
, and we consider any other
Throwable
to be an
error.
Example 8-32. Our specification lambda expressions get passed into the should method
public
public
void
void
should
(
String description
,
Specification specification
) {
try
try
{
Expect expect
=
new
new
Expect
();