Добавил:
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

Listing 5.32. Error - client subscribed more than once

org.mockito.exceptions.verification.TooManyActualInvocations:

clientA.receive(

Mock for Message, hashCode: 29715552

);

Wanted 1 time:

-> at com.practicalunittesting.chp05.raceresults.RaceResultsServiceTest

.shouldSendOnlyOneMessageToMultiSubscriber(RaceResultsServiceTest.java:55) But was 2 times. Undesired invocation:

-> at com.practicalunittesting.chp05.raceresults.RaceResultsService

.send(RaceResultsService.java:23)

This can be fixed by replacing List with Set within the RaceResultsService class, as shown in Listing 5.33.

Listing 5.33. The fourth test has passed!

public class RaceResultsService {

private Collection<Client> clients = new HashSet<Client>();

public void addSubscriber(Client client) { clients.add(client);

}

public void send(Message message) { for (Client client : clients) {

client.receive(message);

}

}

}

A set does not allow for duplicates. The older version - which used a list - did not work properly.

Mockito: How Many Times?

Let us go back to Listing 5.31 for a minute, so that we can learn something new about Mockito. This code contains a single line, which verifies whether the receive() method of the client has been called. To be more precise, it makes Mockito verify whether this method has been called exactly once.

verify(clientA).receive(message);

If you want to specify another value, you could use another static method of the Mockito class: times(), e.g.:

verify(clientA, times(3)).receive(message);

5.4.5. The Fifth Test: Remove a Subscriber

What remains is to make sure that once a client has unsubscribed, it will not receive any messages. Such a test can be built upon the existing tests - i.e. those tests that prove that a subscribed client does receive messages. Its implementation is presented in Listing 5.34.

86

Chapter 5. Mocks, Stubs, Test Spies

Listing 5.34. The fifth test: unsubscribed client stops receiving messages

@Test

public void unsubscribedClientShouldNotReceiveMessages() { raceResults.addSubscriber(clientA); raceResults.removeSubscriber(clientA);

raceResults.send(message);

verify(clientA, never()).receive(message);

}

We know that after this line of code clientA should start receiving messages. Our knowledge is based on other tests (i.e. subscribedClientShouldReceiveMessage()), which verify this behaviour. …but we want the removeSubscriber() method to alter this behaviour (we let IDE auto-generate this method),

…so clientA will not receive any message. Again, this "negative" verification is done using the

never() method.

After we let the IDE create an empty implementation of the removeSubscriber() method of the RaceResultsService class, the test will fail, as shown in Listing 5.35.

Listing 5.35. Error - unsubscribed client still receives messages

org.mockito.exceptions.verification.NeverWantedButInvoked:

clientA.receive(

Mock for Message, hashCode: 19653053

);

Never wanted here:

-> at com.practicalunittesting.chp05.raceresults.RaceResultsServiceTest

.unsubscribedClientShouldNotReceiveMessages(RaceResultsServiceTest.java:63) But invoked here:

-> at com.practicalunittesting.chp05.raceresults.RaceResultsService

.send(RaceResultsService.java:22)

Proper implementation of the removeSubscriber() method is shown in Listing 5.36. Now the test passes.

Listing 5.36. The fifth test has passed: client unsubscribes successfully

public class RaceResultsService {

private Collection<Client> clients = new HashSet<Client>();

public void addSubscriber(Client client) { clients.add(client);

}

public void send(Message message) { for (Client client : clients) {

client.receive(message);

}

}

public void removeSubscriber(Client client) { clients.remove(client);

}

}

The client is removed from the subscribers list.

87

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