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

Chapter 3. Unit Tests with no Collaborators

3.11.3. HashMap

Write unit tests which will verify the following properties of the java.util.HashMap class:

an object stored with the put() method can be retrieved with the get() method,

adding a second object with the same key results in the old value being replaced ,

the clear() method removes all its content,

the null value can be used as a key,

Additional requirements and hints:

use the appropariate JUnit annotations to create a fresh, empty map before each test method is called (see Section 3.8),

Javadocs of the java.util.HashMap class can be accessed at http://download.oracle.com/javase/6/ docs/api/java/util/HashMap.html.

3.11.4. Fahrenheits to Celcius with Parameterized Tests

A simple test class is shown in Listing 3.17. Your task is to introduce parameterized tests, so there will be no repetition in the test code.

Listing 3.17. Fahrenheit to Celcius conversion test

public class FahrenheitCelciusConverterTest {

@Test

public void shouldConvertCelciusToFahrenheit() {

assertEquals(32, FahrenheitCelciusConverter.toFahrenheit(0)); assertEquals(98, FahrenheitCelciusConverter.toFahrenheit(37)); assertEquals(212, FahrenheitCelciusConverter.toFahrenheit(100));

}

@Test

public void shouldConvertFahrenheitToCelcius() { assertEquals(0, FahrenheitCelciusConverter.toCelcius(32));

assertEquals(37, FahrenheitCelciusConverter.toCelcius(100)); assertEquals(100, FahrenheitCelciusConverter.toCelcius(212));

}

}

3.11.5. Master Your IDE

Make sure you spend some time learning about the support your IDE can give you in terms of effective unit testing. In particular, there are two features you should get accustomed with.

Templates

Every decent IDE allows you to create custom templates for quickly creating larger code structures. For the sake of efficient unit testing, you should at least learn how to:

37

Chapter 3. Unit Tests with no Collaborators

create a test class template (so, for example, you have all the required imports already included),

create some typical code constructs - i.e. parameterized tests and set-up methods.

Quick Navigation

It is very handy to able to quickly navigate between the production class (e.g. Money) and the corresponding test classes (e.g. MoneyTest). Find out the proper keyboard shortcut for doing this.

38

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