Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tdd injection workshop


Published on

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

Published in: Technology
  • Be the first to comment

Tdd injection workshop

  1. 1. TDD Injection Workshop<br />Test First Cycle<br />By Orlando Garcia<br />Scrum Tropic Thunder Team<br />1<br />
  2. 2. The business voice<br />We want you to implement a SMS sending feature for our cell phones<br />2<br />
  3. 3. Nowadays tech requirements<br />Your code must be testable, self – <br />documented, bug free, easy to read, clean…<br />3<br />
  4. 4. Story 1: Smssending<br /> <br />As a client, <br />I want to send a Sms<br />So that I don´t have to call the contact<br />4<br />
  5. 5. Scenario 1: Everything is OK<br /> <br />Given the Sms is already composed<br />When client sends the Sms<br />Then “SMS successfully sent” message is displayed<br />And Sent folder contains the sent message<br />5<br />
  6. 6. 6<br />
  7. 7. 7<br />
  8. 8. 8<br />
  9. 9. Test setup<br />Declare and Create the object under test (out)<br />SmsSender sender; // object under test<br />Create a setup method to get a new instance of out for every test scenario<br />@Before // JUnit annotation<br />@BeforeMethod// TestNG annotation<br />9<br />
  10. 10. 10<br />
  11. 11. Write a Test<br />First scenario<br />@Test sendDisplaysSmsSuccessfulySentWhenEverythingIsOk<br />@Test send_EverythingIsOk_SmsSuccessfulySent<br />The most simple form of method under test<br />public void send(); // method under test<br />Assertions. Look for “Then” scenario´sstatements<br />State assertions<br />Behavior verifications<br />importstaticorg.junit.Assert.assertEquals;<br />11<br />
  12. 12. Compile the Test<br />Dependencies declaration and construction<br />By hand<br />new MockObjectX(); // fake object<br />Using mock frameworks (Mockito, JMock, EasyMock, etc.)<br />ObjectToMockobj= mock(ObjectToMock.class);<br />12<br />
  13. 13. Fail the Test<br />Dependency Injection<br />By hand via setter method<br />dependentObj.setDependency(dependencyMock);<br />By hand via constructor method<br />newDependentObj(dependencyMock);<br />Automatically with a DI framework<br />@Autowire; // spring like annotations<br />@Inject; // guice like annotations<br />Run<br />13<br />
  14. 14. Pass the Test<br />Implement object under test<br />It should satisfy test assertions and verifications<br />Improve assertions readability by using hamcrestassertThat<br />importstaticorg.junit.Assert.assertThat;<br />importstaticorg.hamcrest.Matchers.*;<br />Run<br />14<br />
  15. 15. 15<br />
  16. 16. Refactor the object under test<br />Is there a better way to implement it?<br />Any idea??? Look for design flaws (*):<br />Global State aka Singleton<br />Digging into Collaborators<br />Law of Demeter Violation<br />Global reference to time<br />Hard-coded new Operator<br />Lack of Dependency Injection<br />(*) See MiskoHevery guide: Writing Testable Code<br />16<br />
  17. 17. 17<br />
  18. 18. Digging into Collaborators<br />Flaw: the object under test looks itselfvaluesto do itsjob<br />publicvoidsend() {<br />Smssms = smsComposer.getSms();<br />Solution: pass in the real needed object as a method parameter<br />publicvoidsend(Smssms) {<br />Smssms = smsComposer.getSms();<br />18<br />
  19. 19. 19<br />
  20. 20. Scenario 2: Account Low on Credits<br />Given the SMS is already composed<br />And the client’s account is low on credits<br />When client sends the SMS<br />Then “Insufficient credits available” message is displayed<br />And the SMS is stored in Outbox folder<br />20<br />
  21. 21. Scenario 3: Multiple contacts<br />Given the SMS is already composed<br />And it is directed to 10 different contacts<br />When client sends the SMS<br />Then “SMS successfully sent” message is displayed<br />And all 10 SMS are stored in Sent folder<br />21<br />
  22. 22. Scenario 4: Any contact was selected<br />Given the SMS is already composed <br />And there is any contact selected<br />When client sends the SMS<br />Then “Please select a contact” message is displayed<br />And Contacts selector is displayed<br />22<br />
  23. 23. Scenario 5: Message is empty<br />Given the SMS is already composed<br />And the text message is empty<br />When client sends the SMS<br />Then “Write a message before send” message is displayed<br />And the Message Editor is displayed<br />23<br />
  24. 24. Scenario 6: Network Error<br />Given the SMS is already composed<br />And the network service is unavaliable<br />When client sends the SMS<br />Then “Network error” message is displayed<br />And the SMS is stored in Outbox folder<br />24<br />
  25. 25. 25<br />