tdd injection workshop

25
TDD Injection Workshop Test First Cycle By Orlando Garcia Scrum Tropic Thunder Team 1

Upload: orlando-garcia

Post on 15-Jan-2015

429 views

Category:

Technology


0 download

DESCRIPTION

Small workshop that shows fundamental Object Oriented concepts, design and testing

TRANSCRIPT

Page 1: Tdd injection workshop

1

TDD Injection Workshop

Test First CycleBy Orlando Garcia

Scrum Tropic Thunder Team

Page 2: Tdd injection workshop

2

The business voice

We want you to implement a SMS sending feature for our cell phones

Page 3: Tdd injection workshop

3

Nowadays tech requirements

Your code must be testable, self – documented, bug free, easy to read, clean…

Page 4: Tdd injection workshop

4

Story 1: Sms sending

As a client, I want to send a SmsSo that I don´t have to call the contact

Page 5: Tdd injection workshop

5

Scenario 1: Everything is OK

Given the Sms is already composedWhen client sends the SmsThen “SMS successfully sent” message is displayedAnd Sent folder contains the sent message

Page 6: Tdd injection workshop

6

Page 7: Tdd injection workshop

7

Page 8: Tdd injection workshop

8

Page 9: Tdd injection workshop

9

Test setup

• Declare and Create the object under test (out)– SmsSender sender; // object under test

• Create a setup method to get a new instance of out for every test scenario– @Before // JUnit annotation– @BeforeMethod // TestNG annotation

Page 10: Tdd injection workshop

10

Page 11: Tdd injection workshop

11

Write a Test

• First scenario– @Test sendDisplaysSmsSuccessfulySentWhenEverythingIsOk– @Test send_EverythingIsOk_SmsSuccessfulySent

• The most simple form of method under test– public void send(); // method under test

• Assertions. Look for “Then” scenario´s statements– State assertions– Behavior verifications– import static org.junit.Assert.assertEquals;

Page 12: Tdd injection workshop

12

Compile the Test

• Dependencies declaration and construction– By hand

• new MockObjectX(); // fake object

– Using mock frameworks (Mockito, JMock, EasyMock, etc.)• ObjectToMock obj = mock(ObjectToMock.class);

Page 13: Tdd injection workshop

13

Fail the Test

• Dependency Injection– By hand via setter method

• dependentObj.setDependency(dependencyMock);

– By hand via constructor method• new DependentObj(dependencyMock);

– Automatically with a DI framework• @Autowire; // spring like annotations

• @Inject; // guice like annotations

• Run

Page 14: Tdd injection workshop

14

Pass the Test

• Implement object under test– It should satisfy test assertions and verifications– Improve assertions readability by using hamcrest

assertThat• import static org.junit.Assert.assertThat;• import static org.hamcrest.Matchers.*;

• Run

Page 15: Tdd injection workshop

15

Page 16: Tdd injection workshop

16

Refactor the object under test

• Is there a better way to implement it?• Any idea??? Look for design flaws (*):– Global State aka Singleton– Digging into Collaborators– Law of Demeter Violation– Global reference to time– Hard-coded new Operator– Lack of Dependency Injection

– (*) See Misko Hevery guide: Writing Testable Code

Page 17: Tdd injection workshop

17

Page 18: Tdd injection workshop

18

Digging into Collaborators

• Flaw: the object under test looks itself values to do its job– public void send() {

Sms sms = smsComposer.getSms();

• Solution: pass in the real needed object as a method parameter– public void send(Sms sms) {

Sms sms = smsComposer.getSms();

Page 19: Tdd injection workshop

19

Page 20: Tdd injection workshop

20

Scenario 2: Account Low on Credits

Given the SMS is already composedAnd the client’s account is low on creditsWhen client sends the SMSThen “Insufficient credits available” message is

displayedAnd the SMS is stored in Outbox folder

Page 21: Tdd injection workshop

21

Scenario 3: Multiple contacts

Given the SMS is already composedAnd it is directed to 10 different contactsWhen client sends the SMSThen “SMS successfully sent” message is displayedAnd all 10 SMS are stored in Sent folder

Page 22: Tdd injection workshop

22

Scenario 4: Any contact was selected

Given the SMS is already composed And there is any contact selectedWhen client sends the SMSThen “Please select a contact” message is

displayedAnd Contacts selector is displayed

Page 23: Tdd injection workshop

23

Scenario 5: Message is empty

Given the SMS is already composedAnd the text message is emptyWhen client sends the SMSThen “Write a message before send” message is

displayedAnd the Message Editor is displayed

Page 24: Tdd injection workshop

24

Scenario 6: Network Error

Given the SMS is already composedAnd the network service is unavaliableWhen client sends the SMSThen “Network error” message is displayedAnd the SMS is stored in Outbox folder

Page 25: Tdd injection workshop

25