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

Chapter 9. Organization Of Tests

9.8. Exercises

In addition to the exercises presented below, do not forget to read some more about Behaviour Driven Development! Many tools and ideas, especially for integration and end-to-end tests, are based on the BDD approach.

9.8.1. Test Fixture Setting

Enhance the test from Listing 9.17, so some actions are executed:

before the tests of this class are executed,

after the tests of this class have been executed,

before each test method is executed,

after each test method has been executed.

Add some System.out.println() statements (or a logger, if you consider System.out.println() to be lame) to the created method. Execute the test, and observe whether the order of execution is as expected.

Listing 9.17. Test fixture setting

public class TestFixtureTest {

@Test

public void testMethodA() { System.out.println("method A");

}

@Test

public void testMethodB() { System.out.println("method B");

}

}

9.8.2. Test Data Builder

The listing below shows the Transaction class. It is a simple POJO with no logic but only getters and setters methods (which have been omitted in the listing to make it shorter).

212

Chapter 9. Organization Of Tests

Listing 9.18. Transaction class

public class Transaction {

private long id;

private State state;

private boolean retryAllowed;

private String message;

private String billingId;

// getters and setters omitted

...

}

There are four possible values of the state field - PROCESSING, OK, CANCELLED, ERROR.

Write a test which creates a number of objects of the Transaction class using the Test Data Builder pattern. Compare the result with the "normal" approach of creating objects directly within your test code (using their constructor and setters).

213

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