SlideShare una empresa de Scribd logo
1 de 46
Managing Refactoring in a Test Driven World Ben Stopford Royal Bank of Scotland
		“I've always been a old 			fashioned classic TDDer and 		thus far I don't see any 			reason to change. I don't see any compelling benefits for mockist TDD, and am concerned about the consequences of coupling tests to implementation.”
This is about as far from the fence as Martin ever gets!! Mocking, BDD, Interaction based testing. The belief is that they inhibit refactoring because of the coupling they add between test and source. This is what we will be looking at today.
@Test void shouldMoveCashToNewAccount(){ 	Transfertran= new CashTransfer(5,…); tran.execute(newAcc); assertEqual(expected, newAcc.balance()); } Test body that  Exercises the  Class under test State based  assertion
Save Button Save Action Validity Check ORM DB
Save Button Save Action Validity Check ORM DB
Save Button Save Action Validity Check ORM DB Stub n’ State approach class StubValididityCheck{ boolean valid(){            return true;       } }
Save Button Save Action Validity Check ORM DB
o = new ruleComposite(new A(…), new B(…)); result = o.run(user); assertEqual(Result.VALID, result); A B o = new ObjectUnderTest(stubA, stubB); result = o.doSomething(); assertEqual(expected, result); A B
<End-to-End Test> Save Button Save Action Validity Check ORM DB … we need to make sure all the dots tie up! (else our wrapped up units might start to diverge)
This object is easy to test as it is naturally isolated. This object is harder to test in isolation as it forwards calls down the stack.
A Composite Object Behaviour is defined by forwarding calls to composed objects Assert Here No observable change in state Composite
Mocking frameworks automate the testing of the interactions between classes Assert Here Mock based test
o = new RuleComposite(mockRuleA, mockRuleB); check(new Expectation(){ oneof(mockRuleA.run(user));oneof(mockRuleB.run(user)).throws(getExep()); }; o.run(obj); A B Interaction Based Testing Rather than testing changes in state, the interaction between objects are asserted.
How does this relate to the State based testing with Stubs? Both allow us to isolate the code under test. In practice Mocking leads to a very different development process largely because you tend to mock at a class by class level, teasing out roles for collaborating classes.
Mocking facilitates a different development process.  Collaborators do not need to be implemented for the test to pass. They are simply mocked. Mocks used here. No need to develop Classes yet! Develop Class and Test in a tight loop
Refactoring should not change the behavior of a class. Hence state based tests should not break. Refactoring a class may change the way it communicates with collaborating classes, breaking interaction based tests. Increased Coupling makes refactoring harder Stub n’ State Interaction Based Testing
Most horror stories associated with Interaction Based Tests are a result of excessive coupling produced by poor implementation Interaction Based Testing is harder. There is more metaphorical rope.
How Mocking Can Add Unnecessary Coupling? Mocking Value Objects: An orange is always an orange
How Mocking Can Add Unnecessary Coupling Complex Constructors: There’s a test trying to get out
Smaller classes / classes with little functionality increase the support burden needlessly.
For Example the Extract Class refactor Increases the number of interaction points whilst holding the functional content constant
You don’t need to mock everything! Demarcating groups of objects with mocks and using state based testing internally good practice. All interactions are mocked A group of collaborating objects are isolated with mocks. Internally state based testing is used.
Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?
Refactoring tested code - has mocking gone wrong?

Más contenido relacionado

Similar a Refactoring tested code - has mocking gone wrong?

Sample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and MockitoSample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and MockitoTomek Kaczanowski
 
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionDenard Springle IV
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildAndrei Sebastian Cîmpean
 
The Art of Unit Testing Feedback
The Art of Unit Testing FeedbackThe Art of Unit Testing Feedback
The Art of Unit Testing FeedbackDeon Huang
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Advance unittest
Advance unittestAdvance unittest
Advance unittestReza Arbabi
 
Narrow Down What to Test
Narrow Down What to TestNarrow Down What to Test
Narrow Down What to TestZsolt Fabok
 
Bridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous DeliveryBridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous Deliverymasoodjan
 
How to write effective test cases
How to write effective test casesHow to write effective test cases
How to write effective test casesHarshad Ingle
 
Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objectsLim Chanmann
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetdevlabsalliance
 
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...IEEEGLOBALSOFTTECHNOLOGIES
 
Comparable entity mining from comparative questions
Comparable entity mining from comparative questionsComparable entity mining from comparative questions
Comparable entity mining from comparative questionsIEEEFINALYEARPROJECTS
 
Acceptance And Story Testing Patterns - By Charles Bradley
Acceptance And Story Testing Patterns - By Charles BradleyAcceptance And Story Testing Patterns - By Charles Bradley
Acceptance And Story Testing Patterns - By Charles BradleySynerzip
 

Similar a Refactoring tested code - has mocking gone wrong? (20)

Sample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and MockitoSample Chapter of Practical Unit Testing with TestNG and Mockito
Sample Chapter of Practical Unit Testing with TestNG and Mockito
 
Testing And Mxunit In ColdFusion
Testing And Mxunit In ColdFusionTesting And Mxunit In ColdFusion
Testing And Mxunit In ColdFusion
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
Mocking with Mockito
Mocking with MockitoMocking with Mockito
Mocking with Mockito
 
The Art of Unit Testing Feedback
The Art of Unit Testing FeedbackThe Art of Unit Testing Feedback
The Art of Unit Testing Feedback
 
Unit testing 101
Unit testing 101Unit testing 101
Unit testing 101
 
Test Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build AccelerationTest Dependencies and the Future of Build Acceleration
Test Dependencies and the Future of Build Acceleration
 
Testing smells
Testing smellsTesting smells
Testing smells
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Advance unittest
Advance unittestAdvance unittest
Advance unittest
 
Narrow Down What to Test
Narrow Down What to TestNarrow Down What to Test
Narrow Down What to Test
 
Bridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous DeliveryBridging the communication Gap & Continuous Delivery
Bridging the communication Gap & Continuous Delivery
 
How to write effective test cases
How to write effective test casesHow to write effective test cases
How to write effective test cases
 
Interaction testing using mock objects
Interaction testing using mock objectsInteraction testing using mock objects
Interaction testing using mock objects
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...
JAVA 2013 IEEE DATAMINING PROJECT Comparable entity mining from comparative q...
 
Comparable entity mining from comparative questions
Comparable entity mining from comparative questionsComparable entity mining from comparative questions
Comparable entity mining from comparative questions
 
Acceptance And Story Testing Patterns - By Charles Bradley
Acceptance And Story Testing Patterns - By Charles BradleyAcceptance And Story Testing Patterns - By Charles Bradley
Acceptance And Story Testing Patterns - By Charles Bradley
 

Más de Ben Stopford

10 Principals for Effective Event-Driven Microservices with Apache Kafka
10 Principals for Effective Event-Driven Microservices with Apache Kafka10 Principals for Effective Event-Driven Microservices with Apache Kafka
10 Principals for Effective Event-Driven Microservices with Apache KafkaBen Stopford
 
10 Principals for Effective Event Driven Microservices
10 Principals for Effective Event Driven Microservices10 Principals for Effective Event Driven Microservices
10 Principals for Effective Event Driven MicroservicesBen Stopford
 
The Future of Streaming: Global Apps, Event Stores and Serverless
The Future of Streaming: Global Apps, Event Stores and ServerlessThe Future of Streaming: Global Apps, Event Stores and Serverless
The Future of Streaming: Global Apps, Event Stores and ServerlessBen Stopford
 
A Global Source of Truth for the Microservices Generation
A Global Source of Truth for the Microservices GenerationA Global Source of Truth for the Microservices Generation
A Global Source of Truth for the Microservices GenerationBen Stopford
 
Building Event Driven Services with Kafka Streams
Building Event Driven Services with Kafka StreamsBuilding Event Driven Services with Kafka Streams
Building Event Driven Services with Kafka StreamsBen Stopford
 
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with Streams
NDC London 2017  - The Data Dichotomy- Rethinking Data and Services with StreamsNDC London 2017  - The Data Dichotomy- Rethinking Data and Services with Streams
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with StreamsBen Stopford
 
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...Ben Stopford
 
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2:  Building Event-Driven Services with Apache KafkaEvent Driven Services Part 2:  Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2: Building Event-Driven Services with Apache KafkaBen Stopford
 
Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy Ben Stopford
 
Event Driven Services Part 3: Putting the Micro into Microservices with State...
Event Driven Services Part 3: Putting the Micro into Microservices with State...Event Driven Services Part 3: Putting the Micro into Microservices with State...
Event Driven Services Part 3: Putting the Micro into Microservices with State...Ben Stopford
 
Strata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data DichotomyStrata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data DichotomyBen Stopford
 
The Power of the Log
The Power of the LogThe Power of the Log
The Power of the LogBen Stopford
 
Streaming, Database & Distributed Systems Bridging the Divide
Streaming, Database & Distributed Systems Bridging the DivideStreaming, Database & Distributed Systems Bridging the Divide
Streaming, Database & Distributed Systems Bridging the DivideBen Stopford
 
Data Pipelines with Apache Kafka
Data Pipelines with Apache KafkaData Pipelines with Apache Kafka
Data Pipelines with Apache KafkaBen Stopford
 
Microservices for a Streaming World
Microservices for a Streaming WorldMicroservices for a Streaming World
Microservices for a Streaming WorldBen Stopford
 
A little bit of clojure
A little bit of clojureA little bit of clojure
A little bit of clojureBen Stopford
 
Big iron 2 (published)
Big iron 2 (published)Big iron 2 (published)
Big iron 2 (published)Ben Stopford
 
The return of big iron?
The return of big iron?The return of big iron?
The return of big iron?Ben Stopford
 
Big Data & the Enterprise
Big Data & the EnterpriseBig Data & the Enterprise
Big Data & the EnterpriseBen Stopford
 

Más de Ben Stopford (20)

10 Principals for Effective Event-Driven Microservices with Apache Kafka
10 Principals for Effective Event-Driven Microservices with Apache Kafka10 Principals for Effective Event-Driven Microservices with Apache Kafka
10 Principals for Effective Event-Driven Microservices with Apache Kafka
 
10 Principals for Effective Event Driven Microservices
10 Principals for Effective Event Driven Microservices10 Principals for Effective Event Driven Microservices
10 Principals for Effective Event Driven Microservices
 
The Future of Streaming: Global Apps, Event Stores and Serverless
The Future of Streaming: Global Apps, Event Stores and ServerlessThe Future of Streaming: Global Apps, Event Stores and Serverless
The Future of Streaming: Global Apps, Event Stores and Serverless
 
A Global Source of Truth for the Microservices Generation
A Global Source of Truth for the Microservices GenerationA Global Source of Truth for the Microservices Generation
A Global Source of Truth for the Microservices Generation
 
Building Event Driven Services with Kafka Streams
Building Event Driven Services with Kafka StreamsBuilding Event Driven Services with Kafka Streams
Building Event Driven Services with Kafka Streams
 
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with Streams
NDC London 2017  - The Data Dichotomy- Rethinking Data and Services with StreamsNDC London 2017  - The Data Dichotomy- Rethinking Data and Services with Streams
NDC London 2017 - The Data Dichotomy- Rethinking Data and Services with Streams
 
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
Building Event Driven Services with Apache Kafka and Kafka Streams - Devoxx B...
 
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2:  Building Event-Driven Services with Apache KafkaEvent Driven Services Part 2:  Building Event-Driven Services with Apache Kafka
Event Driven Services Part 2: Building Event-Driven Services with Apache Kafka
 
Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy Event Driven Services Part 1: The Data Dichotomy
Event Driven Services Part 1: The Data Dichotomy
 
Event Driven Services Part 3: Putting the Micro into Microservices with State...
Event Driven Services Part 3: Putting the Micro into Microservices with State...Event Driven Services Part 3: Putting the Micro into Microservices with State...
Event Driven Services Part 3: Putting the Micro into Microservices with State...
 
Strata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data DichotomyStrata Software Architecture NY: The Data Dichotomy
Strata Software Architecture NY: The Data Dichotomy
 
The Power of the Log
The Power of the LogThe Power of the Log
The Power of the Log
 
Streaming, Database & Distributed Systems Bridging the Divide
Streaming, Database & Distributed Systems Bridging the DivideStreaming, Database & Distributed Systems Bridging the Divide
Streaming, Database & Distributed Systems Bridging the Divide
 
Data Pipelines with Apache Kafka
Data Pipelines with Apache KafkaData Pipelines with Apache Kafka
Data Pipelines with Apache Kafka
 
JAX London Slides
JAX London SlidesJAX London Slides
JAX London Slides
 
Microservices for a Streaming World
Microservices for a Streaming WorldMicroservices for a Streaming World
Microservices for a Streaming World
 
A little bit of clojure
A little bit of clojureA little bit of clojure
A little bit of clojure
 
Big iron 2 (published)
Big iron 2 (published)Big iron 2 (published)
Big iron 2 (published)
 
The return of big iron?
The return of big iron?The return of big iron?
The return of big iron?
 
Big Data & the Enterprise
Big Data & the EnterpriseBig Data & the Enterprise
Big Data & the Enterprise
 

Refactoring tested code - has mocking gone wrong?

  • 1. Managing Refactoring in a Test Driven World Ben Stopford Royal Bank of Scotland
  • 2. “I've always been a old fashioned classic TDDer and thus far I don't see any reason to change. I don't see any compelling benefits for mockist TDD, and am concerned about the consequences of coupling tests to implementation.”
  • 3. This is about as far from the fence as Martin ever gets!! Mocking, BDD, Interaction based testing. The belief is that they inhibit refactoring because of the coupling they add between test and source. This is what we will be looking at today.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. @Test void shouldMoveCashToNewAccount(){ Transfertran= new CashTransfer(5,…); tran.execute(newAcc); assertEqual(expected, newAcc.balance()); } Test body that Exercises the Class under test State based assertion
  • 12. Save Button Save Action Validity Check ORM DB
  • 13. Save Button Save Action Validity Check ORM DB
  • 14.
  • 15. Save Button Save Action Validity Check ORM DB Stub n’ State approach class StubValididityCheck{ boolean valid(){ return true; } }
  • 16. Save Button Save Action Validity Check ORM DB
  • 17. o = new ruleComposite(new A(…), new B(…)); result = o.run(user); assertEqual(Result.VALID, result); A B o = new ObjectUnderTest(stubA, stubB); result = o.doSomething(); assertEqual(expected, result); A B
  • 18.
  • 19.
  • 20. <End-to-End Test> Save Button Save Action Validity Check ORM DB … we need to make sure all the dots tie up! (else our wrapped up units might start to diverge)
  • 21.
  • 22. This object is easy to test as it is naturally isolated. This object is harder to test in isolation as it forwards calls down the stack.
  • 23.
  • 24. A Composite Object Behaviour is defined by forwarding calls to composed objects Assert Here No observable change in state Composite
  • 25. Mocking frameworks automate the testing of the interactions between classes Assert Here Mock based test
  • 26. o = new RuleComposite(mockRuleA, mockRuleB); check(new Expectation(){ oneof(mockRuleA.run(user));oneof(mockRuleB.run(user)).throws(getExep()); }; o.run(obj); A B Interaction Based Testing Rather than testing changes in state, the interaction between objects are asserted.
  • 27.
  • 28. How does this relate to the State based testing with Stubs? Both allow us to isolate the code under test. In practice Mocking leads to a very different development process largely because you tend to mock at a class by class level, teasing out roles for collaborating classes.
  • 29. Mocking facilitates a different development process. Collaborators do not need to be implemented for the test to pass. They are simply mocked. Mocks used here. No need to develop Classes yet! Develop Class and Test in a tight loop
  • 30.
  • 31.
  • 32.
  • 33. Refactoring should not change the behavior of a class. Hence state based tests should not break. Refactoring a class may change the way it communicates with collaborating classes, breaking interaction based tests. Increased Coupling makes refactoring harder Stub n’ State Interaction Based Testing
  • 34. Most horror stories associated with Interaction Based Tests are a result of excessive coupling produced by poor implementation Interaction Based Testing is harder. There is more metaphorical rope.
  • 35. How Mocking Can Add Unnecessary Coupling? Mocking Value Objects: An orange is always an orange
  • 36. How Mocking Can Add Unnecessary Coupling Complex Constructors: There’s a test trying to get out
  • 37.
  • 38. Smaller classes / classes with little functionality increase the support burden needlessly.
  • 39. For Example the Extract Class refactor Increases the number of interaction points whilst holding the functional content constant
  • 40.
  • 41.
  • 42.
  • 43. You don’t need to mock everything! Demarcating groups of objects with mocks and using state based testing internally good practice. All interactions are mocked A group of collaborating objects are isolated with mocks. Internally state based testing is used.