Java Reference
In-Depth Information
});
});
I appreciate that if you're not familiar with JavaScript, this may seem confusing. We will be
going through the concepts at a gentler pace as we build a Java 8 equivalent next. Just re-
member that the syntax for a lambda expression in JavaScript is
function() { … }
.
Let's take a look at each of the concepts in turn:
▪ Each
spec
describes a single behavior that your program exhibits.
▪ An
expectation
is a way of describing the behavior of the application. You will find ex-
pectations in specs.
▪ Groups of specs are combined into a
suite
.
Each of these concepts has an equivalent in a traditional testing framework, such as JUnit. A
spec is similar to a test method, an expectation is similar to an assertion, and a suite is similar
to a test class.
A DSL in Java
Let's look at an example of what we're aiming for with our Java-based BDD framework.
Example 8-28. Some stories to specify a Stack
public
public class
class
StackSpec
StackSpec
{{
describe
(
"a stack"
,
it
-> {
it
.
should
(
"be empty when created"
,
expect
-> {
expect
.
that
(
new
new
Stack
()).
isEmpty
();
});
it
.
should
(
"push new elements onto the top of the stack"
,
expect
-> {
Stack
<
Integer
>
stack
=
new
new
Stack
<>();
stack
.
push
(
1
);
expect
.
that
(
stack
.
get
(
0
)).
isEqualTo
(
1
);
});
it
.
should
(
"pop the last element pushed onto the stack"
,
expect
-> {
Stack
<
Integer
>
stack
=
new
new
Stack
<>();