Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tomek Kaczanowski - Practical Unit Testing with JUnit and Mockito - 2013.pdf
Скачиваний:
228
Добавлен:
07.03.2016
Размер:
6.59 Mб
Скачать

Chapter 5. Mocks, Stubs, Test Spies

5.7. Exercises

The exercises presented in this section will allow you to practise your knowledge of testing with test doubles through actually using them. They will also allow you to familiarize yourself with the Mockito framework.

5.7.1. User Service Tested

Write a happy-path test for the class presented below. Verify that the user gets his new password, and that the updateUser() method of userDAO is called.

Listing 5.43. The UserServiceImpl class

public class UserServiceImpl {

private UserDAO userDAO;

private SecurityService securityService;

public void assignPassword(User user) throws Exception {

String passwordMd5 = securityService.md5(user.getPassword()); user.setPassword(passwordMd5);

userDAO.updateUser(user);

}

public UserServiceImpl(UserDAO dao, SecurityService security) { this.userDAO = dao;

this.securityService = security;

}

}

5.7.2. Race Results Enhanced

Please enhance the Race Results example (see Section 5.4) with the following functionality:

RaceResultsService should send messages with the results of different categories of race - e.g. horse races, F1 races, boat-races, etc. Subscribers should be able to subscribe to selected categories. Make sure they receive only messages related to the ones they have signed up for.

Each message sent by RaceResultsService should be logged. Introduce a logging DOC, and make sure that the date and text of each message is logged. Do not implement the logging mechanism: concentrate on the interactions between the service and its collaborator.

In the tests implemented so far, RaceResultsService sends only one message. This is unrealistic! Enhance the tests to ensure that subscribers will receive any number of sent messages.

What should happen if a client that is not subscribed tries to unsubscribe? Make up your mind about it, write a test which verifies this behaviour, and make RaceResultsService behave accordingly.

5.7.3. Booking System Revisited

You have already written one booking system (see Section 4.10.3). This time, you are asked to implement a similar application, but testing it using test doubles. Below, you will find a description and some

96

Chapter 5. Mocks, Stubs, Test Spies

requirements that will help you to start coding. If some details are omitted, simply make them up during the development.

This booking system allows classrooms to be booked. Each classroom has a certain capacity (e.g. for 20 people), and can be equipped with an overhead projector, microphone and whiteboard. It also has a certain "name" (ID, number, whatever you wish to call it…). The API of the system should allow one to:

list all existing classrooms,

list all available classrooms (for a given day and hourly time slot),

book a specific classroom by name (e.g. "I want to book classroom A1": book("A1")),

book a specific classroom by specifying some constraints (e.g. "I want to book a classroom with a

projector for 20 people": book(20, Equipment.PROJECTOR)).

Here are some additional constraints, so the system is not too complex at first:

only periodical booking is supported, which means you can book, for example, classroom A1 for every Friday from 10 to 11 am, but not just for Friday the 13th of May.

each booking lasts for 1 hour; no longer or shorter periods are allowed.

Once you have implemented the system specified above, use your imagination and add some more complexity. For example:

each booking operation should be written to logs16,

each classroom has a "cleaning hour", when it is not available,

the booking time is no longer limited to 1 hour

5.7.4. Read, Read, Read!

This is not so much an exercise as a reminder: namely, that you really should read Mockito’s documentation if you plan to use it. This book gives you a good understanding of what Mockito is good for, and explains the core syntax, but does not try to discuss every detail of Mockito’s API. Please, spend some time reading the official Mockito documentation, studying its wiki page and browsing Javadocs! Also, reading some online discussions about differences between Mockito and other frameworks (i.e. EasyMock) may be highly instructive.

16Please read http://www.mockobjects.com/2007/04/test-smell-logging-is-also-feature.html before implementing this feature.

97

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]