SlideShare a Scribd company logo
Just Do It! ColdBox
Integration Testing
A V I R T U A L E X P E R I E N C E
A V I R T U A L E X P E R I E N C E
About Me
• Javier Quintero
• Systems Engineer
• Born in Colombia => Living in
Houston, Texas
• Java and Coldfusion Developer
• Currently working for Ortus
Solutions
A V I R T U A L E X P E R I E N C E
Content
• Introduction
• Testing Foundations
• Intro to TestBox
• BDD Testing
• ColdBox Integration Testing
• Suites and Specs
• Expectations
• Matchers
• MockBox
• Mocking Data
What are Integration Tests?
Integration testing is a level of
software testing where individual
units are combined and tested as
a group. The purpose of this level
of testing is to expose faults in the
interaction between integrated
units. Test drivers and test stubs
are used to assist in Integration
Testing.
A V I R T U A L E X P E R I E N C E
A V I R T U A L E X P E R I E N C E
Why this is important?
• Integration tests allow you to test
your application by covering the
real use cases.
• It’s the extension of unit testing.
• Integration testing takes a
smaller unit of unit testing and
tests their behavior as the whole.
A V I R T U A L E X P E R I E N C E
Advantages of Integration Testing
• Tests run faster compared to end to
end tests.
• Confidence in the development
cycle is high.
• Easy to integrate with daily builds
and easy to test in development
environment.
• Tests are more reliable and easy to
isolate the failures.
• Majorly helps to build real-time use
cases during the end to end testing.
A V I R T U A L E X P E R I E N C E
When should we test?
•Always!
• No more excuses!
• Test your code and make it
part of your development
process.
A V I R T U A L E X P E R I E N C E
How to test your ColdBox App
A V I R T U A L E X P E R I E N C E
> CommandBox to the rescue
• Install TestBox
• install testbox –savedev
TestBox Command Help
• testbox help
TestBox Generations
• testbox create bdd
Even Run Tests
• Testbox run
A V I R T U A L E X P E R I E N C E
ColdBox Testing
• ColdBox has specific testing classes for
• Integration
• Event Handlers
• Interceptors
• Model Objects
Tools
ColdBox Testing
A V I R T U A L E X P E R I E N C E
ColdBox tightly integrates to MockBox
and TestBox
A V I R T U A L E X P E R I E N C E
TestBox
• TestBox is a next generation testing
framework for ColdFusion (CFML) that
is based on BDD (Behavior Driven
Development) for providing a clean
obvious syntax for writing tests.
• TestBox 4.0.0 released last week!
• TestBox Output Utilities
• Mocking Data
A V I R T U A L E X P E R I E N C E
MockBox
• TestBox includes a mocking and
stubbing library called MockBox.
• No need to install a separate libary, it
is part of TestBox!
• MockBox allows you to create mock
and stub objects.
A V I R T U A L E X P E R I E N C E
BDD
• BDD stands for behavior driven
development and is highly based on
creating specifications and
expectations of results in a readable
DSL (Domain Specifc Language).
BDD Relationship
A V I R T U A L E X P E R I E N C E
Requirements to Integration Tests
Req 1
Req 2
Req 3
MyBDDTest
ColdBox Test Suite
ColdBox Application
templates includes a test
folder
• Collection of
• Tests
• Runners
• Automation,
resources
• Includes its own
Application.cfc
A V I R T U A L E X P E R I E N C E
ColdBox Test Classes
A V I R T U A L E X P E R I E N C E
* The BaseTestCase - Used for Integration Testing
A V I R T U A L E X P E R I E N C E
Capabilities
• BaseTestCase gives you the ability to test
your ColdBox Application headlessly
• Test everything top-down: DI, AOP, Cache,
executions, etc.
• Test real requirements: Can I login, Can I
shop, Can I logout
• Tests must have the ability to talk to your
application
• Same server, mappings, orm config, etc
• You can even test relocations
• You can also test view/layout renderings
A V I R T U A L E X P E R I E N C E
Grouping Your Tests
//tests/specs/MySpec.cfc
component extends="testbox.system.BaseSpec"{
// executes before all suites
function beforeAll(){}
// executes after all suites
function afterAll(){}
// All suites go in here
function run( testResults, testBox ){
}
}
• Suites
• Specs
Test Annotations
A V I R T U A L E X P E R I E N C E
A V I R T U A L E X P E R I E N C E
Describing Your Tests
function run( testResults, testBox ){
describe("A suite", function(){
it("contains spec with an awesome expectation", function(){
expect( true ).toBeTrue();
});
it("contains spec with a failure expectation", function(){
expect( true ).toBeFalse();
});
});
}
The describe() function is also aliased with the following names:
story(), feature(), scenario(), given(), when()
Suites: A test suite in TestBox is a collection of specifications that will model what
you want to test.
Specs Arguments
A V I R T U A L E X P E R I E N C E
Full arguments list
A V I R T U A L E X P E R I E N C E
Specs
function run(){
describe("A suite", function(){
it("contains spec with an awesome expectation", function(){
expect( true ).toBeTrue();
});
it("contains a spec with more than 1 expectation", function(){
expect( [1,2,3] ).toBeArray();
expect( [1,2,3] ).toHaveLength( 3 );
});
});
}
A spec is a declaration that will usually test your system with a requirement.
The it() function is also aliased as then()
Specs Arguments
A V I R T U A L E X P E R I E N C E
Full arguments list
A V I R T U A L E X P E R I E N C E
Closure Functions
• They can execute code that is necessary to implement the test.
• CFML rules of scoping are applied to closures.
• Always use the variables scope for easy access and distinction.
function run(){
describe("A suite is a closure", function(){
c = new Calculator();
it("and so is a spec", function(){
expect( c ).toBeTypeOf( 'component' );
});
});
}
A V I R T U A L E X P E R I E N C E
Expectactions
A V I R T U A L E X P E R I E N C E
Expectations
• Self-concatenated strings that evaluate an
actual value to an expected value or
condition.
• expect() -> Takes in a value called the actual
value .
• expectAll() -> Takes in an array or struct
which will be the actual value.
• Concatenate the matchers.
• Multiple evaluations on a single actual value.
A V I R T U A L E X P E R I E N C E
Matchers
Each matcher implements a comparison or evaluation of the actual value and an
expected value or condition.
function run(){
describe("The 'toBe' matcher evaluates equality", function(){
it("and has a positive case", function(){
expect( true ).toBe( true );
});
it("and has a negative case", function(){
expect( false ).notToBe( true );
});
});
describe("Collection expectations", function(){
it( "can be done easily with TestBox", function(){
expectAll( {a:2,b:4,c:6} ).toSatisfy( function(x){ return 0 == x%2; });
});
});
}
Matchers
A V I R T U A L E X P E R I E N C E
Most common matchers
• toBeTrue( [message] ) : value to true
• toBeFalse( [message] ) : value to be false
• toBe( expected, [message] ) : Assert something is equal to each other, no case is required
• toBeWithCase( expected, [message] ) : Expects with case
• toBeNull( [message] ) : Expects the value to be null
• toBeInstanceOf( class, [message] ) : To be the class instance passed
• toMatch( regex, [message] ) : Matches a string with no case-sensitivity
• toMatchWithCase( regex, [message] ) : Matches with case-sensitivity
• toBeTypeOf( type, [message] ) : Assert the type of the incoming actual data, it uses the internal
ColdFusion isValid() function behind the scenes, type can be array, binary, boolean, component,
date, time, float, numeric, integer, query, string, struct, url, uuid plus all the ones from isValid()
• toBe({type}( [message] ) : Same as above but more readable method name. Example: .toBeStruct(),
.toBeArray()
• toBeEmpty( [message] ) : Tests if an array or struct or string or query is empty
• toHaveKey( key, [message] ) : Tests the existence of one key in a structure or hash map
Each matcher implements a comparison or evaluation of the actual value and an expected value or
condition.
Matchers
A V I R T U A L E X P E R I E N C E
Most common matchers
• toHaveDeepKey( key, [message] ) : Assert that a given key exists in the passed in struct by
searching the entire nested structure
• toHaveLength( length, [message] ) : Assert the size of a given string, array, structure or query
• toThrow( [type], [regex], [message] );
• toBeCloseTo( expected, delta, [datepart], [message] ) : Can be used to approximate numbers or
dates according to the expected and delta arguments. For date ranges use the datepart values.
• toBeBetween( min, max, [message] ) : Assert that the passed in actual number or date is between
the passed in min and max values
• toInclude( needle, [message] ) : Assert that the given "needle" argument exists in the incoming
string or array with no case-sensitivity, needle in a haystack anyone?
• toIncludeWithCase( needle, [message] ) : Assert that the given "needle" argument exists in the
incoming string or array with case-sensitivity, needle in a haystack anyone?
• toBeGT( target, [message] ) : Assert that the actual value is greater than the target value
• toBeGTE( target, [message] ) : Assert that the actual value is greater than or equal the target value
• toBeLT( target, [message] ) : Assert that the actual value is less than the target value
• toBeLTE( target, [message] ) : Assert that the actual value is less than or equal the target value
The complete list can be found in the official API Docs site
http://apidocs.ortussolutions.com/testbox/current
A V I R T U A L E X P E R I E N C E
Custom Matchers
• Create your own matcher if you don’t find it in the Docs
boolean function MyMatcher( required expectation, args={} )
boolean function reallyFalse( expectation, args={} ){
expectation.message = (
structKeyExists( args, "message" ) ? args.message :
”[#expectation.actual#] is not really false"
);
if( expectation.isNot )
return ( expectation.actual eq true );
else
return ( expectation.actual eq false );
}
}
A V I R T U A L E X P E R I E N C E
Custom Matchers
• Create your own matcher if you don’t find it in the Docs
boolean function MyMatcher( required expectation, args={} )
boolean function reallyFalse( expectation, args={} ){
expectation.message = (
structKeyExists( args, "message" ) ? args.message :
”[#expectation.actual#] is not really false"
);
if( expectation.isNot )
return ( expectation.actual eq true );
else
return ( expectation.actual eq false );
}
}
A V I R T U A L E X P E R I E N C E
The setup() Method
• This is how ColdBox simulates a request
• Must be called in a setup() or beforeEach()
context
• If not, everything looks like the same request
function run(){
describe( "Settings Suite", function(){
beforeEach( function( currentSpec ){
setup();
} );
});
}
Setup()
Test1
Test3
Test4
Test2
A V I R T U A L E X P E R I E N C E
The execute() Method
The execute() method is your way of making requests in to your
ColdBox application.
Create your test or spec
Set FORM/URL scopes or Headers to simulate
incoming data
Execute the event, it returns an event object (Request
Context)
Do your expectations/assertions
The execute() Method
A V I R T U A L E X P E R I E N C E
ican take the following parameters:
The Handler To Test
A V I R T U A L E X P E R I E N C E
component extends="coldbox.system.EventHandler"{
// Default Action
function index( event, rc, prc ){
prc.welcomeMessage = "Welcome to ITB 2020!";
event.setView( "main/index” );
}
// Do something
function doSomething( event, rc, prc ){
relocate( "main.index” );
}
}
Main handler
The Integration Test
A V I R T U A L E X P E R I E N C E
function run(){
describe( "Main Handler", function(){
beforeEach(function( currentSpec ){
setup();
});
it( "+homepage renders", function(){
var event = execute( event="main.index", renderResults=true );
expect( event.getValue( name="welcomemessage", private=true ) ).toBe( "Welcome to ITB 2020!" );
});
it( "+doSomething relocates", function(){
var event = execute( event="main.doSomething" );
expect(event.getValue( "relocate_event", "" ) ).toBe( "main.index" );
});
});
}
Main handler
A V I R T U A L E X P E R I E N C E
Mocking Data
A V I R T U A L E X P E R I E N C E
Mocking Data
• MockDataCFC dependency included since
version 4.x
• Generate fake data as JSON REST service, a
ColdBox Module or a simple CFC Service API
• MockDataCFC allows you to define the
return data model in a very deterministic and
simple modeling DSL.
Mocking Types
A V I R T U A L E X P E R I E N C E
MockDataCFC supports the following types
• age: Generates a random "adult" age of 18 to 75.
• all_age: Generates a random age of 1 to 100.
• autoincrement: Returns an incremented index starting from 1
• baconlorem: Returns bacon lorem ipsum text. If used as baconlorem:N, returns N paragraphs. If
used as baconlorem:X Y, returns a random number of paragraphs between X and Y.
• date: Generates a random date
• datetime: Generates a random date and time value
• email: Generates a random email.
• fname: Generates a random first name.
• imageurl : Generates a random image URL with a random protocol
• imageurl_http : Generates a random image URL with http only protocol
• imageurl_https : Generates a random image URL with https only protocol
• ipaddress : Generates an ipv4 address
• name: Generates a random name.
• lname: Generates a random last name.
• lorem: Returns lorem ipsum text. If used as lorem:N, returns N paragraphs. If used as lorem:X Y,
returns a random number of paragraphs between X and Y.
• num: By default, a number from 1 to 10. You can also use the form num:X for a random number
between 1 and X. Or num:X Y for a random number between X and Y.
Mocking Types
A V I R T U A L E X P E R I E N C E
More types…
• One of:X y: Requires you to pass N values after it delimited by a colon. Example: oneof:male:
female. Will return a random value from that list.
• rnd:N, rand:N, rnd:X y, rand:X y : Generate random numbers with a specific range or range cap.
• sentence: Generates a sentences. If used as sentence:N, returns N sentences. If used as
sentence:X Y, returns a random number of sentences beetween X and Y.
• ssn: Generates a random Social Security number.
• string: Generates a random string of length 10 by default. You can increase the length by passing it
string:length.
• tel: Generates a random (American) telephone number.
• uuid: Generates a random UUID
• url : Generates a random URL with a random protocol
• url_http : Generates a random URL with http only protocol
• url_https : Generates a random URL with https only protocol
• website : Generates a random website with random protocol
• website_http : Generates a random website, http only protocol
• website_https : Generates a random website, https only protocol
• words: Generates a single word. If used as word:N, returns N words. If used as words:X Y, returns a
random number of words between X and Y.
A V I R T U A L E X P E R I E N C E
Mocking Examples
A V I R T U A L E X P E R I E N C E
Tests Output Utilities
Produce output from your tests
Let’s Put All Together
A V I R T U A L E X P E R I E N C E
A V I R T U A L E X P E R I E N C E
Create The Integration Test
• Let’s use Commandbox to generate the
integration tests for us.
• coldbox create integration-test handler=main actions=show,create,delete
A V I R T U A L E X P E R I E N C E
Life Cycle Methods
A V I R T U A L E X P E R I E N C E
Test suite and specs
A V I R T U A L E X P E R I E N C E
Let’s create our integration test
for the show action
A V I R T U A L E X P E R I E N C E
Let’s create our integration test
for the create action
A V I R T U A L E X P E R I E N C E
Let’s create our integration test
for the delete action
A V I R T U A L E X P E R I E N C E
Run Your Tests
A V I R T U A L E X P E R I E N C E
Gracias!
Javier Quintero
Web Developer
Ortus Solutions, Corp
A V I R T U A L E X P E R I E N C E
@xavikintero
@ortussolutions

More Related Content

PPTX
Test in action – week 1
KEY
Developer testing 101: Become a Testing Fanatic
PPTX
Pragmatic unittestingwithj unit
PPTX
How Scala promotes TDD
PDF
Example First / A Sane Test-Driven Approach to Programming
PPTX
Test in action week 4
PPTX
TDD Training
PDF
Why Your Test Suite Sucks - PHPCon PL 2015
Test in action – week 1
Developer testing 101: Become a Testing Fanatic
Pragmatic unittestingwithj unit
How Scala promotes TDD
Example First / A Sane Test-Driven Approach to Programming
Test in action week 4
TDD Training
Why Your Test Suite Sucks - PHPCon PL 2015

What's hot (19)

PPT
20111018 boost and gtest
PDF
Breaking Dependencies to Allow Unit Testing
PDF
JUnit Kung Fu: Getting More Out of Your Unit Tests
PDF
Tests and Testability: Apex Structure and Strategy
ODP
Interaction testing using mock objects
PPTX
Oleksandr Valetskyy - Increase the quality of your code with property-based t...
PPTX
Qunit Java script Un
PDF
«Как сделать так, чтобы тесты на Swift не причиняли боль» Сычев Александр, Ra...
PPTX
Effective Readable unit testing with junit5
PDF
C++ Unit Test with Google Testing Framework
KEY
How To Test Everything
PDF
Effective Unit Testing
PDF
Test driven development
PDF
Testing akka-actors
PDF
Gallio Crafting A Toolchain
PDF
Modern Python Testing
PDF
Lecture: Advanced Reflection. MetaLinks
PDF
Testing most things in JavaScript - LeedsJS 31/05/2017
PPT
Google mock for dummies
20111018 boost and gtest
Breaking Dependencies to Allow Unit Testing
JUnit Kung Fu: Getting More Out of Your Unit Tests
Tests and Testability: Apex Structure and Strategy
Interaction testing using mock objects
Oleksandr Valetskyy - Increase the quality of your code with property-based t...
Qunit Java script Un
«Как сделать так, чтобы тесты на Swift не причиняли боль» Сычев Александр, Ra...
Effective Readable unit testing with junit5
C++ Unit Test with Google Testing Framework
How To Test Everything
Effective Unit Testing
Test driven development
Testing akka-actors
Gallio Crafting A Toolchain
Modern Python Testing
Lecture: Advanced Reflection. MetaLinks
Testing most things in JavaScript - LeedsJS 31/05/2017
Google mock for dummies
Ad

Similar to Just Do It! ColdBox Integration Testing (20)

PDF
Evolve your coding with some BDD
PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
PDF
CBDW2014 - Behavior Driven Development with TestBox
PDF
BDD Testing and Automating from the trenches - Presented at Into The Box June...
PDF
ITB2016 -BDD testing and automation from the trenches
PDF
Testing - How Vital and How Easy to use
PDF
I am afraid of no test! The power of BDD
PDF
Test box bdd
PDF
Into The Box 2018 | Assert control over your legacy applications
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
PDF
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
PDF
3 WAYS TO TEST YOUR COLDFUSION API
PDF
3 WAYS TO TEST YOUR COLDFUSION API -
PPTX
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
PPTX
Techorama 2017 - Testing the unit, and beyond.
PPTX
Test Driven Development - a Practitioner’s Perspective
PPTX
Java Script Isn\'t a Toy Anymore
PPTX
Understanding TDD - theory, practice, techniques and tips.
PDF
Software testing: an introduction - 2017
PDF
Automated testing in javascript
Evolve your coding with some BDD
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
CBDW2014 - Behavior Driven Development with TestBox
BDD Testing and Automating from the trenches - Presented at Into The Box June...
ITB2016 -BDD testing and automation from the trenches
Testing - How Vital and How Easy to use
I am afraid of no test! The power of BDD
Test box bdd
Into The Box 2018 | Assert control over your legacy applications
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Into...
Introduction to Unit Testing, BDD and Mocking using TestBox & MockBox at Adob...
3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API -
3 Ways to test your ColdFusion API - 2017 Adobe CF Summit
Techorama 2017 - Testing the unit, and beyond.
Test Driven Development - a Practitioner’s Perspective
Java Script Isn\'t a Toy Anymore
Understanding TDD - theory, practice, techniques and tips.
Software testing: an introduction - 2017
Automated testing in javascript
Ad

More from Ortus Solutions, Corp (20)

PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
PDF
June Webinar: BoxLang-Dynamic-AWS-Lambda
PDF
BoxLang-Dynamic-AWS-Lambda by Luis Majano.pdf
PDF
What's-New-with-BoxLang-Brad Wood.pptx.pdf
PDF
Getting Started with BoxLang - CFCamp 2025.pdf
PDF
CFCamp2025 - Keynote Day 1 led by Luis Majano.pdf
PDF
What's New with BoxLang Led by Brad Wood.pdf
PDF
Vector Databases and the BoxLangCFML Developer.pdf
PDF
Using cbSSO in a ColdBox App Led by Jacob Beers.pdf
PDF
Use JSON to Slash Your Database Performance.pdf
PDF
Portable CI wGitLab and Github led by Gavin Pickin.pdf
PDF
Tame the Mesh An intro to cross-platform tracing and troubleshooting.pdf
PDF
Supercharging CommandBox with Let's Encrypt.pdf
PDF
Spice up your site with cool animations using GSAP..pdf
PDF
Passkeys and cbSecurity Led by Eric Peterson.pdf
PDF
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
PDF
Integrating the OpenAI API in Your Coldfusion Apps.pdf
PDF
Hidden Gems in FusionReactor for BoxLang, ACF, and Lucee Users.pdf
PDF
Geting-started with BoxLang Led By Raymon Camden.pdf
PDF
From Zero to CRUD with ORM - Led by Annette Liskey.pdf
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
June Webinar: BoxLang-Dynamic-AWS-Lambda
BoxLang-Dynamic-AWS-Lambda by Luis Majano.pdf
What's-New-with-BoxLang-Brad Wood.pptx.pdf
Getting Started with BoxLang - CFCamp 2025.pdf
CFCamp2025 - Keynote Day 1 led by Luis Majano.pdf
What's New with BoxLang Led by Brad Wood.pdf
Vector Databases and the BoxLangCFML Developer.pdf
Using cbSSO in a ColdBox App Led by Jacob Beers.pdf
Use JSON to Slash Your Database Performance.pdf
Portable CI wGitLab and Github led by Gavin Pickin.pdf
Tame the Mesh An intro to cross-platform tracing and troubleshooting.pdf
Supercharging CommandBox with Let's Encrypt.pdf
Spice up your site with cool animations using GSAP..pdf
Passkeys and cbSecurity Led by Eric Peterson.pdf
Legacy Code Nightmares , Hellscapes, and Lessons Learned.pdf
Integrating the OpenAI API in Your Coldfusion Apps.pdf
Hidden Gems in FusionReactor for BoxLang, ACF, and Lucee Users.pdf
Geting-started with BoxLang Led By Raymon Camden.pdf
From Zero to CRUD with ORM - Led by Annette Liskey.pdf

Recently uploaded (20)

PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
cuic standard and advanced reporting.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
“AI and Expert System Decision Support & Business Intelligence Systems”
cuic standard and advanced reporting.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
20250228 LYD VKU AI Blended-Learning.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Just Do It! ColdBox Integration Testing

  • 1. Just Do It! ColdBox Integration Testing A V I R T U A L E X P E R I E N C E
  • 2. A V I R T U A L E X P E R I E N C E About Me • Javier Quintero • Systems Engineer • Born in Colombia => Living in Houston, Texas • Java and Coldfusion Developer • Currently working for Ortus Solutions
  • 3. A V I R T U A L E X P E R I E N C E Content • Introduction • Testing Foundations • Intro to TestBox • BDD Testing • ColdBox Integration Testing • Suites and Specs • Expectations • Matchers • MockBox • Mocking Data
  • 4. What are Integration Tests? Integration testing is a level of software testing where individual units are combined and tested as a group. The purpose of this level of testing is to expose faults in the interaction between integrated units. Test drivers and test stubs are used to assist in Integration Testing. A V I R T U A L E X P E R I E N C E
  • 5. A V I R T U A L E X P E R I E N C E Why this is important? • Integration tests allow you to test your application by covering the real use cases. • It’s the extension of unit testing. • Integration testing takes a smaller unit of unit testing and tests their behavior as the whole.
  • 6. A V I R T U A L E X P E R I E N C E Advantages of Integration Testing • Tests run faster compared to end to end tests. • Confidence in the development cycle is high. • Easy to integrate with daily builds and easy to test in development environment. • Tests are more reliable and easy to isolate the failures. • Majorly helps to build real-time use cases during the end to end testing.
  • 7. A V I R T U A L E X P E R I E N C E When should we test? •Always! • No more excuses! • Test your code and make it part of your development process.
  • 8. A V I R T U A L E X P E R I E N C E How to test your ColdBox App
  • 9. A V I R T U A L E X P E R I E N C E > CommandBox to the rescue • Install TestBox • install testbox –savedev TestBox Command Help • testbox help TestBox Generations • testbox create bdd Even Run Tests • Testbox run
  • 10. A V I R T U A L E X P E R I E N C E ColdBox Testing • ColdBox has specific testing classes for • Integration • Event Handlers • Interceptors • Model Objects
  • 11. Tools ColdBox Testing A V I R T U A L E X P E R I E N C E ColdBox tightly integrates to MockBox and TestBox
  • 12. A V I R T U A L E X P E R I E N C E TestBox • TestBox is a next generation testing framework for ColdFusion (CFML) that is based on BDD (Behavior Driven Development) for providing a clean obvious syntax for writing tests. • TestBox 4.0.0 released last week! • TestBox Output Utilities • Mocking Data
  • 13. A V I R T U A L E X P E R I E N C E MockBox • TestBox includes a mocking and stubbing library called MockBox. • No need to install a separate libary, it is part of TestBox! • MockBox allows you to create mock and stub objects.
  • 14. A V I R T U A L E X P E R I E N C E BDD • BDD stands for behavior driven development and is highly based on creating specifications and expectations of results in a readable DSL (Domain Specifc Language).
  • 15. BDD Relationship A V I R T U A L E X P E R I E N C E Requirements to Integration Tests Req 1 Req 2 Req 3 MyBDDTest
  • 16. ColdBox Test Suite ColdBox Application templates includes a test folder • Collection of • Tests • Runners • Automation, resources • Includes its own Application.cfc A V I R T U A L E X P E R I E N C E
  • 17. ColdBox Test Classes A V I R T U A L E X P E R I E N C E * The BaseTestCase - Used for Integration Testing
  • 18. A V I R T U A L E X P E R I E N C E Capabilities • BaseTestCase gives you the ability to test your ColdBox Application headlessly • Test everything top-down: DI, AOP, Cache, executions, etc. • Test real requirements: Can I login, Can I shop, Can I logout • Tests must have the ability to talk to your application • Same server, mappings, orm config, etc • You can even test relocations • You can also test view/layout renderings
  • 19. A V I R T U A L E X P E R I E N C E Grouping Your Tests //tests/specs/MySpec.cfc component extends="testbox.system.BaseSpec"{ // executes before all suites function beforeAll(){} // executes after all suites function afterAll(){} // All suites go in here function run( testResults, testBox ){ } } • Suites • Specs
  • 20. Test Annotations A V I R T U A L E X P E R I E N C E
  • 21. A V I R T U A L E X P E R I E N C E Describing Your Tests function run( testResults, testBox ){ describe("A suite", function(){ it("contains spec with an awesome expectation", function(){ expect( true ).toBeTrue(); }); it("contains spec with a failure expectation", function(){ expect( true ).toBeFalse(); }); }); } The describe() function is also aliased with the following names: story(), feature(), scenario(), given(), when() Suites: A test suite in TestBox is a collection of specifications that will model what you want to test.
  • 22. Specs Arguments A V I R T U A L E X P E R I E N C E Full arguments list
  • 23. A V I R T U A L E X P E R I E N C E Specs function run(){ describe("A suite", function(){ it("contains spec with an awesome expectation", function(){ expect( true ).toBeTrue(); }); it("contains a spec with more than 1 expectation", function(){ expect( [1,2,3] ).toBeArray(); expect( [1,2,3] ).toHaveLength( 3 ); }); }); } A spec is a declaration that will usually test your system with a requirement. The it() function is also aliased as then()
  • 24. Specs Arguments A V I R T U A L E X P E R I E N C E Full arguments list
  • 25. A V I R T U A L E X P E R I E N C E Closure Functions • They can execute code that is necessary to implement the test. • CFML rules of scoping are applied to closures. • Always use the variables scope for easy access and distinction. function run(){ describe("A suite is a closure", function(){ c = new Calculator(); it("and so is a spec", function(){ expect( c ).toBeTypeOf( 'component' ); }); }); }
  • 26. A V I R T U A L E X P E R I E N C E Expectactions
  • 27. A V I R T U A L E X P E R I E N C E Expectations • Self-concatenated strings that evaluate an actual value to an expected value or condition. • expect() -> Takes in a value called the actual value . • expectAll() -> Takes in an array or struct which will be the actual value. • Concatenate the matchers. • Multiple evaluations on a single actual value.
  • 28. A V I R T U A L E X P E R I E N C E Matchers Each matcher implements a comparison or evaluation of the actual value and an expected value or condition. function run(){ describe("The 'toBe' matcher evaluates equality", function(){ it("and has a positive case", function(){ expect( true ).toBe( true ); }); it("and has a negative case", function(){ expect( false ).notToBe( true ); }); }); describe("Collection expectations", function(){ it( "can be done easily with TestBox", function(){ expectAll( {a:2,b:4,c:6} ).toSatisfy( function(x){ return 0 == x%2; }); }); }); }
  • 29. Matchers A V I R T U A L E X P E R I E N C E Most common matchers • toBeTrue( [message] ) : value to true • toBeFalse( [message] ) : value to be false • toBe( expected, [message] ) : Assert something is equal to each other, no case is required • toBeWithCase( expected, [message] ) : Expects with case • toBeNull( [message] ) : Expects the value to be null • toBeInstanceOf( class, [message] ) : To be the class instance passed • toMatch( regex, [message] ) : Matches a string with no case-sensitivity • toMatchWithCase( regex, [message] ) : Matches with case-sensitivity • toBeTypeOf( type, [message] ) : Assert the type of the incoming actual data, it uses the internal ColdFusion isValid() function behind the scenes, type can be array, binary, boolean, component, date, time, float, numeric, integer, query, string, struct, url, uuid plus all the ones from isValid() • toBe({type}( [message] ) : Same as above but more readable method name. Example: .toBeStruct(), .toBeArray() • toBeEmpty( [message] ) : Tests if an array or struct or string or query is empty • toHaveKey( key, [message] ) : Tests the existence of one key in a structure or hash map Each matcher implements a comparison or evaluation of the actual value and an expected value or condition.
  • 30. Matchers A V I R T U A L E X P E R I E N C E Most common matchers • toHaveDeepKey( key, [message] ) : Assert that a given key exists in the passed in struct by searching the entire nested structure • toHaveLength( length, [message] ) : Assert the size of a given string, array, structure or query • toThrow( [type], [regex], [message] ); • toBeCloseTo( expected, delta, [datepart], [message] ) : Can be used to approximate numbers or dates according to the expected and delta arguments. For date ranges use the datepart values. • toBeBetween( min, max, [message] ) : Assert that the passed in actual number or date is between the passed in min and max values • toInclude( needle, [message] ) : Assert that the given "needle" argument exists in the incoming string or array with no case-sensitivity, needle in a haystack anyone? • toIncludeWithCase( needle, [message] ) : Assert that the given "needle" argument exists in the incoming string or array with case-sensitivity, needle in a haystack anyone? • toBeGT( target, [message] ) : Assert that the actual value is greater than the target value • toBeGTE( target, [message] ) : Assert that the actual value is greater than or equal the target value • toBeLT( target, [message] ) : Assert that the actual value is less than the target value • toBeLTE( target, [message] ) : Assert that the actual value is less than or equal the target value The complete list can be found in the official API Docs site http://apidocs.ortussolutions.com/testbox/current
  • 31. A V I R T U A L E X P E R I E N C E Custom Matchers • Create your own matcher if you don’t find it in the Docs boolean function MyMatcher( required expectation, args={} ) boolean function reallyFalse( expectation, args={} ){ expectation.message = ( structKeyExists( args, "message" ) ? args.message : ”[#expectation.actual#] is not really false" ); if( expectation.isNot ) return ( expectation.actual eq true ); else return ( expectation.actual eq false ); } }
  • 32. A V I R T U A L E X P E R I E N C E Custom Matchers • Create your own matcher if you don’t find it in the Docs boolean function MyMatcher( required expectation, args={} ) boolean function reallyFalse( expectation, args={} ){ expectation.message = ( structKeyExists( args, "message" ) ? args.message : ”[#expectation.actual#] is not really false" ); if( expectation.isNot ) return ( expectation.actual eq true ); else return ( expectation.actual eq false ); } }
  • 33. A V I R T U A L E X P E R I E N C E The setup() Method • This is how ColdBox simulates a request • Must be called in a setup() or beforeEach() context • If not, everything looks like the same request function run(){ describe( "Settings Suite", function(){ beforeEach( function( currentSpec ){ setup(); } ); }); } Setup() Test1 Test3 Test4 Test2
  • 34. A V I R T U A L E X P E R I E N C E The execute() Method The execute() method is your way of making requests in to your ColdBox application. Create your test or spec Set FORM/URL scopes or Headers to simulate incoming data Execute the event, it returns an event object (Request Context) Do your expectations/assertions
  • 35. The execute() Method A V I R T U A L E X P E R I E N C E ican take the following parameters:
  • 36. The Handler To Test A V I R T U A L E X P E R I E N C E component extends="coldbox.system.EventHandler"{ // Default Action function index( event, rc, prc ){ prc.welcomeMessage = "Welcome to ITB 2020!"; event.setView( "main/index” ); } // Do something function doSomething( event, rc, prc ){ relocate( "main.index” ); } } Main handler
  • 37. The Integration Test A V I R T U A L E X P E R I E N C E function run(){ describe( "Main Handler", function(){ beforeEach(function( currentSpec ){ setup(); }); it( "+homepage renders", function(){ var event = execute( event="main.index", renderResults=true ); expect( event.getValue( name="welcomemessage", private=true ) ).toBe( "Welcome to ITB 2020!" ); }); it( "+doSomething relocates", function(){ var event = execute( event="main.doSomething" ); expect(event.getValue( "relocate_event", "" ) ).toBe( "main.index" ); }); }); } Main handler
  • 38. A V I R T U A L E X P E R I E N C E Mocking Data
  • 39. A V I R T U A L E X P E R I E N C E Mocking Data • MockDataCFC dependency included since version 4.x • Generate fake data as JSON REST service, a ColdBox Module or a simple CFC Service API • MockDataCFC allows you to define the return data model in a very deterministic and simple modeling DSL.
  • 40. Mocking Types A V I R T U A L E X P E R I E N C E MockDataCFC supports the following types • age: Generates a random "adult" age of 18 to 75. • all_age: Generates a random age of 1 to 100. • autoincrement: Returns an incremented index starting from 1 • baconlorem: Returns bacon lorem ipsum text. If used as baconlorem:N, returns N paragraphs. If used as baconlorem:X Y, returns a random number of paragraphs between X and Y. • date: Generates a random date • datetime: Generates a random date and time value • email: Generates a random email. • fname: Generates a random first name. • imageurl : Generates a random image URL with a random protocol • imageurl_http : Generates a random image URL with http only protocol • imageurl_https : Generates a random image URL with https only protocol • ipaddress : Generates an ipv4 address • name: Generates a random name. • lname: Generates a random last name. • lorem: Returns lorem ipsum text. If used as lorem:N, returns N paragraphs. If used as lorem:X Y, returns a random number of paragraphs between X and Y. • num: By default, a number from 1 to 10. You can also use the form num:X for a random number between 1 and X. Or num:X Y for a random number between X and Y.
  • 41. Mocking Types A V I R T U A L E X P E R I E N C E More types… • One of:X y: Requires you to pass N values after it delimited by a colon. Example: oneof:male: female. Will return a random value from that list. • rnd:N, rand:N, rnd:X y, rand:X y : Generate random numbers with a specific range or range cap. • sentence: Generates a sentences. If used as sentence:N, returns N sentences. If used as sentence:X Y, returns a random number of sentences beetween X and Y. • ssn: Generates a random Social Security number. • string: Generates a random string of length 10 by default. You can increase the length by passing it string:length. • tel: Generates a random (American) telephone number. • uuid: Generates a random UUID • url : Generates a random URL with a random protocol • url_http : Generates a random URL with http only protocol • url_https : Generates a random URL with https only protocol • website : Generates a random website with random protocol • website_http : Generates a random website, http only protocol • website_https : Generates a random website, https only protocol • words: Generates a single word. If used as word:N, returns N words. If used as words:X Y, returns a random number of words between X and Y.
  • 42. A V I R T U A L E X P E R I E N C E Mocking Examples
  • 43. A V I R T U A L E X P E R I E N C E Tests Output Utilities Produce output from your tests
  • 44. Let’s Put All Together A V I R T U A L E X P E R I E N C E
  • 45. A V I R T U A L E X P E R I E N C E Create The Integration Test • Let’s use Commandbox to generate the integration tests for us. • coldbox create integration-test handler=main actions=show,create,delete
  • 46. A V I R T U A L E X P E R I E N C E Life Cycle Methods
  • 47. A V I R T U A L E X P E R I E N C E Test suite and specs
  • 48. A V I R T U A L E X P E R I E N C E Let’s create our integration test for the show action
  • 49. A V I R T U A L E X P E R I E N C E Let’s create our integration test for the create action
  • 50. A V I R T U A L E X P E R I E N C E Let’s create our integration test for the delete action
  • 51. A V I R T U A L E X P E R I E N C E Run Your Tests
  • 52. A V I R T U A L E X P E R I E N C E Gracias!
  • 53. Javier Quintero Web Developer Ortus Solutions, Corp A V I R T U A L E X P E R I E N C E @xavikintero @ortussolutions