SlideShare a Scribd company logo
1 of 8
Software
               Develop
Engineers




QA            Manual Tests




                                      Testin
Test        Automated Tests   Tools     g
Engineers
                                      Magic
1) Do work in constructor.
   Work in the constructor such as:
    creating/initializing collaborators,
    communicating with other services,
   Logic to set up its own state removes seams
    needed for testing, forcing subclasses/mocks
    to inherit unwanted behavior. Too much work
    in the constructor prevents instantiation or
    altering collaborators in the test.
   The LawOfDemeter specifies a style guideline:
    "Only talk to your immediate friends."

   Objects are passed in but never used directly
    (only used to get access to other objects)
   JUnit is a unit testing framework for the Java
    programming language. JUnit has been
    important in the development of test-driven
    development.
import org.junit.*;
public class TestFoobar{
@Before public void setUp() throws Exception {}
@Test public void testSomething () {
//This piece will use asserts to verify.
}
@After public void tearDown() throws
  Exception{}
}
Before: Hard to Test                 // An attempted test that
                                     becomes pretty hard
/* Basic new operators called        class HouseTest extends TestCase
directly in the class constructor.   {
(Forever preventing a seam to
create different kitchen and         public void testThisIsReallyHard() {
bedroom collaborators). */
                                     House house = new House();
class House {                        /* I'm stuck with those Kitchen
Kitchen kitchen = new Kitchen();     and Bedroom objects created in
Bedroom bedroom;                     the constructor. */
House() {                            }
bedroom = new Bedroom();             }
}

More Related Content

What's hot

Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnitAktuğ Urun
 
Cactus in container_unit_test_framework
Cactus in container_unit_test_frameworkCactus in container_unit_test_framework
Cactus in container_unit_test_frameworkKenan Sevindik
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mockPranalee Rokde
 
Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3Vasil Remeniuk
 
Java - Singleton Pattern
Java - Singleton PatternJava - Singleton Pattern
Java - Singleton PatternCharles Casadei
 
Complementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an ExampleComplementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an ExamplePVS-Studio
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introductionDenis Bazhin
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Ivan Ivanov
 

What's hot (20)

Junit
JunitJunit
Junit
 
Constructor
ConstructorConstructor
Constructor
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Java Unit Test - JUnit
Java Unit Test - JUnitJava Unit Test - JUnit
Java Unit Test - JUnit
 
Cactus in container_unit_test_framework
Cactus in container_unit_test_frameworkCactus in container_unit_test_framework
Cactus in container_unit_test_framework
 
Test ng
Test ngTest ng
Test ng
 
All about unit testing using (power) mock
All about unit testing using (power) mockAll about unit testing using (power) mock
All about unit testing using (power) mock
 
Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3Scala laboratory: Globus. iteration #3
Scala laboratory: Globus. iteration #3
 
Java - Singleton Pattern
Java - Singleton PatternJava - Singleton Pattern
Java - Singleton Pattern
 
Junit
JunitJunit
Junit
 
Complementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an ExampleComplementing Unit Testing with Static Analysis, with NUnit as an Example
Complementing Unit Testing with Static Analysis, with NUnit as an Example
 
TestNG vs Junit
TestNG vs JunitTestNG vs Junit
TestNG vs Junit
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
Calculon
CalculonCalculon
Calculon
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
Intro to junit
Intro to junitIntro to junit
Intro to junit
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Introduction to JUnit
Introduction to JUnitIntroduction to JUnit
Introduction to JUnit
 
Test doubles
Test doublesTest doubles
Test doubles
 

Viewers also liked (17)

RESUMS DE LA LLEGENDA "La primera mata d'arròs"
RESUMS DE LA LLEGENDA "La primera mata d'arròs"RESUMS DE LA LLEGENDA "La primera mata d'arròs"
RESUMS DE LA LLEGENDA "La primera mata d'arròs"
 
Article 8
Article  8Article  8
Article 8
 
Actes comitè verd 2015 16 1r trimestre
Actes comitè verd 2015 16 1r trimestreActes comitè verd 2015 16 1r trimestre
Actes comitè verd 2015 16 1r trimestre
 
less freamwork
less freamworkless freamwork
less freamwork
 
Article 8
Article  8Article  8
Article 8
 
Kajian tindakan
Kajian tindakanKajian tindakan
Kajian tindakan
 
Ci fitxes de formes geomètriques de la natura
Ci fitxes de formes geomètriques de la naturaCi fitxes de formes geomètriques de la natura
Ci fitxes de formes geomètriques de la natura
 
less freamwork
less freamworkless freamwork
less freamwork
 
Observem
Observem Observem
Observem
 
Proposta deures estiu 2015 16
Proposta deures estiu 2015 16Proposta deures estiu 2015 16
Proposta deures estiu 2015 16
 
PDF Valldemossa (Mallorca)
PDF Valldemossa (Mallorca)PDF Valldemossa (Mallorca)
PDF Valldemossa (Mallorca)
 
Llegenda la primera mata d'arròs
Llegenda la primera mata d'arròsLlegenda la primera mata d'arròs
Llegenda la primera mata d'arròs
 
Maintaining Customer Loyalty
Maintaining Customer LoyaltyMaintaining Customer Loyalty
Maintaining Customer Loyalty
 
Kim Peterson's Visual Resume
Kim Peterson's Visual ResumeKim Peterson's Visual Resume
Kim Peterson's Visual Resume
 
Chapter7 Innovative Education
Chapter7 Innovative EducationChapter7 Innovative Education
Chapter7 Innovative Education
 
Laporan hari guru 2013
Laporan hari guru 2013Laporan hari guru 2013
Laporan hari guru 2013
 
მექანიკური მუშაობა
მექანიკური მუშაობამექანიკური მუშაობა
მექანიკური მუშაობა
 

Similar to Writing testable code

Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testingalessiopace
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAhmed Ehab AbdulAziz
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdfgauravavam
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testingmarkstory
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightOpenDaylight
 
A fresh look at Java Enterprise Application testing with Arquillian
A fresh look at Java Enterprise Application testing with ArquillianA fresh look at Java Enterprise Application testing with Arquillian
A fresh look at Java Enterprise Application testing with ArquillianVineet Reynolds
 
Software Testing
Software TestingSoftware Testing
Software TestingAdroitLogic
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Javakjkleindorfer
 
J unit presentation
J unit presentationJ unit presentation
J unit presentationPriya Sharma
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014FalafelSoftware
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the UntestableMark Baker
 

Similar to Writing testable code (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Embrace Unit Testing
Embrace Unit TestingEmbrace Unit Testing
Embrace Unit Testing
 
An Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDDAn Introduction To Unit Testing and TDD
An Introduction To Unit Testing and TDD
 
31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf31b - JUnit and Mockito.pdf
31b - JUnit and Mockito.pdf
 
Win at life with unit testing
Win at life with unit testingWin at life with unit testing
Win at life with unit testing
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Introduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylightIntroduction to JUnit testing in OpenDaylight
Introduction to JUnit testing in OpenDaylight
 
L08 Unit Testing
L08 Unit TestingL08 Unit Testing
L08 Unit Testing
 
A fresh look at Java Enterprise Application testing with Arquillian
A fresh look at Java Enterprise Application testing with ArquillianA fresh look at Java Enterprise Application testing with Arquillian
A fresh look at Java Enterprise Application testing with Arquillian
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Android testing
Android testingAndroid testing
Android testing
 
Week9 Intro to classes and objects in Java
Week9 Intro to classes and objects in JavaWeek9 Intro to classes and objects in Java
Week9 Intro to classes and objects in Java
 
J unit presentation
J unit presentationJ unit presentation
J unit presentation
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 

Recently uploaded

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Writing testable code

  • 1.
  • 2. Software Develop Engineers QA Manual Tests Testin Test Automated Tests Tools g Engineers Magic
  • 3. 1) Do work in constructor.
  • 4. Work in the constructor such as: creating/initializing collaborators, communicating with other services,  Logic to set up its own state removes seams needed for testing, forcing subclasses/mocks to inherit unwanted behavior. Too much work in the constructor prevents instantiation or altering collaborators in the test.
  • 5. The LawOfDemeter specifies a style guideline: "Only talk to your immediate friends."  Objects are passed in but never used directly (only used to get access to other objects)
  • 6. JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development.
  • 7. import org.junit.*; public class TestFoobar{ @Before public void setUp() throws Exception {} @Test public void testSomething () { //This piece will use asserts to verify. } @After public void tearDown() throws Exception{} }
  • 8. Before: Hard to Test // An attempted test that becomes pretty hard /* Basic new operators called class HouseTest extends TestCase directly in the class constructor. { (Forever preventing a seam to create different kitchen and public void testThisIsReallyHard() { bedroom collaborators). */ House house = new House(); class House { /* I'm stuck with those Kitchen Kitchen kitchen = new Kitchen(); and Bedroom objects created in Bedroom bedroom; the constructor. */ House() { } bedroom = new Bedroom(); } }