This repository contains training materials used as part of the Serenity Dojo mentoring programme.
The goal of the exercises and demos you'll find here is to help you learn more about all the different Test Doubles and become more comfortable with using tools such as Mockito.
The serenitylabs.tutorials.testdoubles.classic
package contains five classes demonstrating when and how to use:
All the five test doubles have been implemented using plain-old Java to help you understand their inner workings better.
- "The Little Mocker" by Robert C. Martin
- "xUnit Test Patterns - Test Double" by Gerard Meszaros
Now that you understand how the test doubles work, try to implement them yourself using Mockito using the "classic" implementation as a reference.
There are five empty test classes waiting for you at
serenitylabs.tutorials.testdoubles.mockito
to give you a place to start.
Now that you're warmed up it's time for something slightly more ambitious - the Stock Broker kata.
A client can interact with the Stock Broker using a text terminal.
To place a single order, the client would type:
GOOG 300 829.08 B
The order command is composed of the following four parts:
- ticker symbol, such as
GOOG
- quantity, such as
300
- price in USD, such as
829.08
- type of order - Buy (
B
) or Sell (S
)
The client could also place an order covering multiple stocks.
A multi-order is a coma-separated list of regular orders:
ZNGA 1300 2.78 B,AAPL 50 139.78 B,FB 320 137.17 S
HINT For the purpose of this exercise it's fine to assume that the input data provided by the user is always well-formed and correct.
The domain is modelled using three main classes:
Client
- The client receives a string representation of the order, parses it and asks theStockBroker
to execute the order.StockBroker
- The stock broker interacts with the stock exchange. He's responsible for producing anOrderSummary
to tell the client how much was spent on buying and selling the stocks.StockExchange
- You should assume that the stock exchange connects to the real stock exchange and any order placed here will be executed.
The goal of this kata is to implement the Client
and the StockBroker
(and potentially some other classes that you think
you'll need) so that when the Client
receives a command such as this one:
GOOG 300 829.08 B
it responds with an order summary presenting the total amount of money spent and earned (assuming the order was executed correctly by the exchange):
Buy: USD 248724.00, Sell: USD 0.00
An empty order is not treated as an error and should produce the following summary:
input:
output: Buy: USD 0.00, Sell: USD 0.00
For a single, correctly executed order we'd have:
input: GOOG 300 829.08 B
output: USD 248724.00, Sell: USD 0.00
And for a multi-order:
input: ZNGA 1300 2.78 B,AAPL 50 139.78 B,FB 320 137.17 S
output: Buy: USD 10603.00, Sell: USD 43894.40
If any of the orders could not be executed by the exchange, the summary should mention this as well.
For example, assuming the exchange could not execute the GOOG
order:
input: GOOG 300 829.08 B
output: USD 0.00, Sell: USD 0.00, Failed: GOOG
And if several stocks failed (for example FB
and ORCL
), but other worked fine:
input: ZNGA 1300 2.78 B,AAPL 50 139.78 B,FB 320 137.17 S,ORCL 1000 42.69 S
output: Buy: USD 10603.00, Sell: USD 00.40, Failed: FB, ORCL
Design your solution starting from the outside - start with the ClientTest
and work your way down, providing real implementations or mocks of the dependencies when needed.
The Client
class uses an implementation of the OrderParser
interface to convert the input received
from the user into a list of Order
objects.
If you like playing with regular expressions or want to increase the level of difficulty of this exercise you might want to implement the parser from scratch.
If you'd rather focus on mocking it's perfectly fine to use the
OptimisticOrderParser
,
which you'll find in the stockbroker.parser
package.
This exercise uses the Joda-Money implementation of the Money class.
To initialise the OptimisticOrderParser
with the correct currency unit use CurrencyUnit.USD
.
Have fun!
Jan and the Serenity Team