23
Test Driven Development on Force.com projects Myths and Truths March 10, 2016

Test Driven Development (TDD) on Force.com projects

Embed Size (px)

Citation preview

Test Driven Development on Force.com projectsMyths and TruthsMarch 10, 2016

Aldo Fernandez Technical Architect

@aldoforce

Safe Harbor

Safe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services.

The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, risks associated with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-K for the most recent fiscal quarter ended July 31, 2011. This document and others are available on the SEC Filings section of the Investor Information section of our Web site.

Any unreleased services or features referenced in this or other press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

Agenda

▪ What is TDD?▪ TDD Process▪ An Apex TDD Example▪ Benefits▪ Challenges

What is TDD? (I)

▪ …but first, what IS NOT TDD:▪ TDD is not BDD (Behavior Driven Development)▪ BDD: specs define tests, and then test defines code implementation▪ BDD in Apex: Specs -> Test class -> class▪ Cucumber (cucumber.io) based projects (Pickle, Pepino, …) which runs

acceptance tests defined in Gherkin▪ …so, what IS TDD?

What is TDD? (II)

▪ It refers to a style of programming where three activities are tightly interwoven:▪ Coding▪ Testing (in the form of unit tests)▪ Design (in the form of refactoring)

What is TDD? (III)

“TDD is a software development approach where tests are written before the functional code”

“TDD is a technique of using automated unit tests to drive the design of software and force decoupling of dependencies”

The TDD process

1. Write a test2. Run all tests (the new should fail)3. Write some code4. Run tests5. If tests passes, refactor6. Repeat step 1

Unit Tests vs Integration Tests

▪ Unit Test▪ Verify that a relatively small piece of

code is doing what is intended to do▪ Narrow in scope▪ Easy to write and execute▪ Test useful for the developer▪ No dependencies on external

components▪ External components are mocked or

stubbed out▪ Test internal consistency

▪ Integration Tests▪ Verify that different pieces of the

system works correctly together.▪ Covers whole business

requirements/features▪ Require more resources to complete▪ Test useful for business▪ Dependencies are required because

the holistic approch.▪ Test consistency between

components

An Apex TDD example (I)

▪ Aldo’s Algebra (don’t google… it’s just a trivial example!)▪ Given 2 integer numbers (a, b) the sum operation will be a + b▪ if one of the numbers is null, treat it as zero

▪ examples▪ AA.sum(2, 3) = 5▪ AA.sum(2, null) = 2▪ AA.sum(null, null) = 0

An Apex TDD example (II)

An Apex TDD example (III)

An Apex TDD example (IV)

Repeat the process until complete the spec

…but what if I have dependencies on other classes/components?

An Apex TDD example (V)

AlgebraController <<class>>

AlgebraServices <<class>>

Algebra <<page>>

An Apex TDD example (VI)

Dependency Injection

An Apex TDD example (VII)

An Apex TDD example (VIII)

Benefits

▪ Forces good architecture design▪ Reduce time of bug hunting▪ Creates a detailed specification▪ Write shorter and less complex code▪ Fewer system.debug() statements ;-)▪ Instant feedback on broken code▪ Code coverage on steroids (goodbye less than 75%)▪ Enforce KISS and YAGNI▪ Tests run faster▪ Mocking around SOQL and DML to avoid declarative changes failures \o/

Challenges

▪ Learning curve▪ Cost of TDD vs Project Budget▪ ROI with TDD▪ Advanced concepts

▪ Dependency Injection▪ Mocking

▪ Cultural issues▪ Handling business requirements changes

Resources + Kudos

▪ Test Driven Development Method in Software Development Process. Denis Duka, Lovre Hribar 2010

▪ Test Driven Development in Apex by Chris Aldridge (@caldrig3)▪ Cucumber (cucumber.io)▪ Pickle, a lightweight Cucumber implementation for Salesforce.com, github.com/lpoulain/pickle ▪ Pepino, BDD framework for Apex. github.com/pbattisson/pepino▪ Unit Testing, Apex Enterprise Patterns and ApexMock, Andrew Fawcett (@andyinthecloud)▪ Writing true unit tests using dependency injection mocking apex, Alex Tennant (@adtennant)▪ ApexMocks: How Does It Work? Jesse Altman (@jessealtman)▪ Inversion of Control Containers and the Dependency Injection Pattern. Martin Fowler, 2004▪ Demo Code repo: https://github.com/aldoforce/uysdug_tdd_demo

Thank You!

Test Driven Development on Force.com projects

Myths and Truths

Uruguay Salesforce Developer GroupMarch 10, 2016

#UYSDUG

Aldo FernandezTechnical Architect

@aldoforce

#uysdug#tdd#dependencyInjection#apex#testing