SlideShare a Scribd company logo
Testing and MXUnitIn ColdFusionBy DenardSpringleJuly 2011, NVCFUG
A testing framework for use in testing components (cfc’s)One piece of an integrated development methodology (e.g. Hudson, Ant and MXUnit)One tool in the Test Driven developers toolkitOne tool in the Object-Oriented developers toolkitIt is not useful for testing non-component templates or outputHTMLJavascript/AjaxCSSEtc. What is MXUnit?
A well organized SDLC environment includes:One or more development servers where code is developed and debugged by developers.One or more testing servers where code is tested using MXUnit (and/or other test frameworks), and is used by QA for regression testing.One or more production servers where fully tested and certified code is released.Dedicated testing servers prevent:Developers from crashing the test serverProduction being used to test code which mayleave datasources in an unknown stateWriting complex extended DAO testing frameworksTest Environment
Download the latest MXUnit Framework. Unzip into webrootTest the install: http://myserver/mxunit/index.cfmWebroot is the root directory of the website you are testing. Installing MXUnit
The TestCase file is a component (a .cfc file) The filename either starts or ends with "Test" The component extends mxunit.framework.TestCase or extends a component that eventually extends mxunit.framework.TestCaseThe TestCase can contain setUp() and tearDown() functions that will run prior to and aftereach and everytestThe TestCase can contain beforeTests() and afterTests() functions that will be run oncebefore and onceafteralltests Part One Anatomy of a Test Case
The TestCase can contain any number of public methods. Those public methods are considered tests regardless of their name, and MXUnit will run them as tests. Any private methods are not considered tests and will not be run by MXUnitFailures will be recorded as failures; errors as errors; and successes as successes Inside of your tests, you make assertions on the results of functions that you call on your component under test Part Two Anatomy of a Test Case
To very quickly get started, you can run the test by loading it in the browser and suffixing it with "?method=runTestRemote", like so: http://localhost/tests/MyTest.cfc?method=runTestRemoteExample TestCase:Part Three Anatomy of a Test Case
Write the test firstWatch the test failWrite the componentWatch the test passTest Driven Development allows you to write your test cases before you begin development.
One caveat to TDD is the components you developare only as good as the tests you write. So getgood at writing tests if you want to use TDD.By Kent Beck Test Driven Development (TDD)
The core of unit testing, i.e. Assertions, are simple: You assert something, and if that assertion does not result in your expectation, the test fails. All code AFTER a failed assertion will NOT execute. Repeat: A failed assertion means the end of that test.This is why it's best practice to not load each test function with lots of assertions. A single failed assertion means that any subsequent assertions in a test will not execute and thus you won't know if those assertions indicate further problems in your code.In other words, one assertion per test function is strongly suggestedas¡sert - to demonstrate the existence of Assertions
assertTrue(boolean condition [,String message])assertTrue is one of the two "bread and butter" assertions in any testing framework. The philosophy is simple: you assert something to be true; if it's not, the test failsassertEquals (expected,actual[, String message])assertEquals is the other core assertion. Here, you're asserting that some actual result equals some expected result. This could be two numbers, two strings, two structs, whatever. For now, MXUnit follows the JUnit pattern of using a single assertEquals to compare any type of data.assertFalse(booleancondition [, String message]) Built-In Assertions
assertSame (obj1,obj2 [,String message])Used to determine if the obj1 and obj2 refer to the same instance.Note that arrays in Adobe ColdFusion are passed by value, so, this will always fail for arrays.assertNotSame (obj1,obj2 [,String message])Used to determine if the obj1 and obj2 do not refer to the same instance.Note that arrays in Adobe ColdFusion are passed by value, so, this will always pass for arrays. Built-In Assertions
assertIsTypeOf (component obj, String type)Determines if obj is of type. type needs to be fully qualified (i.e. ‘core.beans.User’).assertIsXMLDoc (any xml [, String message])Determines if xml is a valid XML DOM object.assertIsArray (any obj1)Determines if the obj1 is a valid array.assertIsDefined*(any obj1)Determines if the obj1 has been defined in the available scope.These are extensions to the base set of assertions and are specific to ColdFusion.MXUnitAssertion Extensions
assertIsEmpty (any obj1)Determines if the obj1 is a 0 length string or NULL equivalent.assertIsEmptyArray (any obj1,[String message])Determines if the obj1 is an array with no elements.assertIsEmptyQuery(any obj1,[String message])Determines if the obj1 is a query with no rows.assertIsEmptyStruct (any obj1,[String message])Determines if the obj1 is a struct with no keys or values.MXUnitAssertion Extensions
assertIsQuery (any q)Determines if q is a valid ColdFusion query.assertIsStruct (any obj)Determines if obj is a valid ColdFusion structure.MXUnitAssertion Extensions
The steps for creating your custom assertion are as follows:Write a test for your assertionWrite the assertionDecide how you want to load it: Always or only on selected tests.Assertion Rules:Your assertion will need to throw mxunit.exception.AssertionFailedError or use an existing assertion that throws this exception.If you want to have optional first or last parameter message, you will need to call normalizeArguments(arguments) in your code.Provides an easy way to add custom assertions to your tests without having to change the mxunit core. Custom Assertions
Data driven testing allows you to execute tests with a wide variety of input data. This can make creating and executing certain kinds of tests efficient and very powerful. Essentially, you provide a reference to a collection of data and MXUnit will iterate over that data and execute the test for each item in the collection.Data ProvidersArrayQueryListExcel/CSVIteratormxunit:dataprovider=“<data provider>”With MXUnitdataproviders Data Driven Testing
TransactionsThe goal is to have a way to execute code that utilizes database logic (inserts, updates, deletes, etc.), test the results of the code, and set the state of the database back to a known state. Leveraging transactions is one way to achieve this. The process is to execute code and after assertions, roll back any transactions. Sounds simple ... it is and it isn't. ColdFusion is limited with transaction handling - you cannot have nested transactions and you need to use the <cftransaction ...> tag.A Test Adapter Pattern for DAO TestingDAO Test Adapter Pattern
Write the test firstExtend the DAO component under test to DAOTestAdapter, or similarRefactorour DAO component: extracting the database logic to a package access method with no transaction handlingCode this packageaccessed method within the transaction block of the public DAO methodCreate a method in DAOTestAdapter that overrides the DAO method under test and calls the package access method, but rolls back the transaction no matter whatRun the test. The expected behavior is that data is inserted into the db, and the method returns trueA Test Adapter Pattern for DAO TestingDAO Test Adapter Pattern
Resulting-State AssertionThe resulting-state assertion tests data. It says "I'm doing something to my object that will change that object's data, or 'state'. I'm going to test that the resulting state of my object is as I expect". A simple example is the common "bank account" or "transaction" example: You have two accounts, you transfer $20 from one account to another, and you test that the first account is 20 bucks shorter and the second account has that 20 bucks.Different instances, same dataThe different-instances, same-data pattern is common in DAO testing. Essentially, we're asserting that two objects are different instances but contain the same data. In MXUnit, you can test for "different instance" by using the assertNotSame() assertion.**The terms here are taken from the outstanding book "Test-Driven" by LasseKoskela. Assertion Patterns
Guard AssertionThe guard assertion is simply a slight variant on the resulting state assertion; typically, the difference is that toward the top of the test, before you get into the "guts" of your assertion(s), you check the object for some condition that you want to ensure exists before proceeding with the meat of your tests. Think of it as "If this condition isn't true, I want to fail right now because the rest of the tests don't matter". Usually the "guard" is just a simple assertion for equality, often to check that a "default" condition exists.Interaction AssertionWith interaction assertions, we're testing to make sure an object and a collaborator worked together as we expected. A great example of an interaction is a "bean" style object, like perhaps a "User", and the DAO for that object, like a UserDAO Assertion Patterns
"Delta" AssertionSometimes you can't assert an absolute equality (like "My list is now 5 elements long"). Sometimes, you have to assert equality relative to some previous state. Imagine you're hooking into some scheduling mechanism, for example. We don't know exactly what getTotalScheduled() will return at any given test run. Maybe it's 1. Maybe it's 30. Who knows. What we want to test is that when we schedule one additional thing, our scheduler's "totalScheduled" count increases by 1.This type of assertion, where we compare the state right before and right after performing some task, is called "delta", or difference, assertion. Assertion Patterns
MXUnit was built to make it as easy as possible to create tests and test suites.The steps for creating and running a TestSuite are:Create a ColdFusion page to run the suiteCreate a TestSuite objectTell the TestSuite what tests to addrun() the TestSuitePrint the outputRun the suite in your web browserA TestSuite is a collection of tests that logically fit together.Creating and Running a TestSuite

More Related Content

KEY
What is Object Oriented CSS?
Nicole Sullivan
 
PPT
Understanding XML DOM
Om Vikram Thapa
 
PPTX
Xml ppt
seemadav1
 
PDF
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
ODP
Introduction to jQuery
manugoel2003
 
PDF
Module 3: Working with the DOM and jQuery
Daniel McGhan
 
PDF
HTML Dasar : #9 Tabel
Sandhika Galih
 
What is Object Oriented CSS?
Nicole Sullivan
 
Understanding XML DOM
Om Vikram Thapa
 
Xml ppt
seemadav1
 
JavaScript Basics and Best Practices - CC FE & UX
JWORKS powered by Ordina
 
Introduction to jQuery
manugoel2003
 
Module 3: Working with the DOM and jQuery
Daniel McGhan
 
HTML Dasar : #9 Tabel
Sandhika Galih
 

What's hot (20)

PDF
Practical Celery
Cameron Maske
 
PDF
Angular dĂŠvelopper des applications .pdf
imenhamada17
 
PDF
React – ¿Qué es React.js?
Gorka MagaĂąa
 
PDF
React Context API
NodeXperts
 
PDF
Hacking ansible
bcoca
 
PDF
BEM it! Introduction to BEM methodology
Varya Stepanova
 
PDF
YEDI VITTHITHE ADI KOSTHAAM
Shalem Arasavelli
 
PDF
HTML Dasar : #4 Paragraf
Sandhika Galih
 
PPTX
Html presentation
Prashanthi Mamidisetty
 
PPTX
React workshop
Imran Sayed
 
PPT
Real World MongoDB: Use Cases from Financial Services by Daniel Roberts
MongoDB
 
PPT
Php
Ajaigururaj R
 
PPTX
Dasar fronte-end vs back-end Developer
alfi setyadi
 
PPTX
React JS - Parte 1
Bruno CatĂŁo
 
PPT
Learning Html
Damian Gonz
 
PPT
De Forms a Oracle Fusion Middleware
JC_Diaz_Belmonte
 
PDF
ప్రకటన గ్రంధము. pdf
Dr. Johnson Satya
 
PPTX
CSS Flexbox (flexible box layout)
Woodridge Software
 
PPTX
Introduction to PHP.pptx
SherinRappai
 
PDF
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
Practical Celery
Cameron Maske
 
Angular dĂŠvelopper des applications .pdf
imenhamada17
 
React – ¿Qué es React.js?
Gorka MagaĂąa
 
React Context API
NodeXperts
 
Hacking ansible
bcoca
 
BEM it! Introduction to BEM methodology
Varya Stepanova
 
YEDI VITTHITHE ADI KOSTHAAM
Shalem Arasavelli
 
HTML Dasar : #4 Paragraf
Sandhika Galih
 
Html presentation
Prashanthi Mamidisetty
 
React workshop
Imran Sayed
 
Real World MongoDB: Use Cases from Financial Services by Daniel Roberts
MongoDB
 
Dasar fronte-end vs back-end Developer
alfi setyadi
 
React JS - Parte 1
Bruno CatĂŁo
 
Learning Html
Damian Gonz
 
De Forms a Oracle Fusion Middleware
JC_Diaz_Belmonte
 
ప్రకటన గ్రంధము. pdf
Dr. Johnson Satya
 
CSS Flexbox (flexible box layout)
Woodridge Software
 
Introduction to PHP.pptx
SherinRappai
 
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
Ad

Similar to Testing And Mxunit In ColdFusion (20)

PPTX
Java Unit Test - JUnit
Aktuğ Urun
 
PPTX
Unit Testing in Java
Ahmed M. Gomaa
 
DOCX
Test Driven Development
Anand Kumar Rajana
 
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
PDF
Effective Unit Test Style Guide
Jacky Lai
 
PPTX
Unit testing
Pooya Sagharchiha
 
PPTX
2.Python_Testing_Using_PyUnit_PyTest.pptx
Ganesh Bhosale
 
PDF
Mastering Assertions in Automation Testing, Importance and Best Practices.pdf
pcloudy2
 
PPT
Junit
Vivek Kulkarni
 
PDF
J unit a starter guide
selfishson83
 
PPT
Mxunit
VIkas Patel
 
PPS
J unit presentation
Priya Sharma
 
PPS
JUnit Presentation
priya_trivedi
 
PPT
Unit testing
Murugesan Nataraj
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
PDF
Intro to Unit Testing in AngularJS
Jim Lynch
 
PDF
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
PPTX
Angular Unit Testing
Alessandro Giorgetti
 
PPTX
unit 1 (1).pptx
SumitKumar918321
 
PPTX
Mocking with Mockito
Paul Churchward
 
Java Unit Test - JUnit
Aktuğ Urun
 
Unit Testing in Java
Ahmed M. Gomaa
 
Test Driven Development
Anand Kumar Rajana
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
All Things Open
 
Effective Unit Test Style Guide
Jacky Lai
 
Unit testing
Pooya Sagharchiha
 
2.Python_Testing_Using_PyUnit_PyTest.pptx
Ganesh Bhosale
 
Mastering Assertions in Automation Testing, Importance and Best Practices.pdf
pcloudy2
 
J unit a starter guide
selfishson83
 
Mxunit
VIkas Patel
 
J unit presentation
Priya Sharma
 
JUnit Presentation
priya_trivedi
 
Unit testing
Murugesan Nataraj
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Intro to Unit Testing in AngularJS
Jim Lynch
 
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
Angular Unit Testing
Alessandro Giorgetti
 
unit 1 (1).pptx
SumitKumar918321
 
Mocking with Mockito
Paul Churchward
 
Ad

More from Denard Springle IV (8)

PPTX
ColdFusion_Code_Security_Best_Practices_NCDevCon_2015
Denard Springle IV
 
PPTX
Team CF Advance Introduction
Denard Springle IV
 
PPTX
Touch Screen Desktop Applications
Denard Springle IV
 
PPTX
jQuery, CSS3 and ColdFusion
Denard Springle IV
 
PDF
ColdFusion Coding Guidelines
Denard Springle IV
 
PPTX
Securing Your Web Applications in ColdFusion
Denard Springle IV
 
PPS
ColdFusion ORM
Denard Springle IV
 
PPTX
Caching & Performance In Cold Fusion
Denard Springle IV
 
ColdFusion_Code_Security_Best_Practices_NCDevCon_2015
Denard Springle IV
 
Team CF Advance Introduction
Denard Springle IV
 
Touch Screen Desktop Applications
Denard Springle IV
 
jQuery, CSS3 and ColdFusion
Denard Springle IV
 
ColdFusion Coding Guidelines
Denard Springle IV
 
Securing Your Web Applications in ColdFusion
Denard Springle IV
 
ColdFusion ORM
Denard Springle IV
 
Caching & Performance In Cold Fusion
Denard Springle IV
 

Recently uploaded (20)

PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
A Day in the Life of Location Data - Turning Where into How.pdf
Precisely
 
The Future of Artificial Intelligence (AI)
Mukul
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 

Testing And Mxunit In ColdFusion

  • 1. Testing and MXUnitIn ColdFusionBy DenardSpringleJuly 2011, NVCFUG
  • 2. A testing framework for use in testing components (cfc’s)One piece of an integrated development methodology (e.g. Hudson, Ant and MXUnit)One tool in the Test Driven developers toolkitOne tool in the Object-Oriented developers toolkitIt is not useful for testing non-component templates or outputHTMLJavascript/AjaxCSSEtc. What is MXUnit?
  • 3. A well organized SDLC environment includes:One or more development servers where code is developed and debugged by developers.One or more testing servers where code is tested using MXUnit (and/or other test frameworks), and is used by QA for regression testing.One or more production servers where fully tested and certified code is released.Dedicated testing servers prevent:Developers from crashing the test serverProduction being used to test code which mayleave datasources in an unknown stateWriting complex extended DAO testing frameworksTest Environment
  • 4. Download the latest MXUnit Framework. Unzip into webrootTest the install: http://myserver/mxunit/index.cfmWebroot is the root directory of the website you are testing. Installing MXUnit
  • 5. The TestCase file is a component (a .cfc file) The filename either starts or ends with "Test" The component extends mxunit.framework.TestCase or extends a component that eventually extends mxunit.framework.TestCaseThe TestCase can contain setUp() and tearDown() functions that will run prior to and aftereach and everytestThe TestCase can contain beforeTests() and afterTests() functions that will be run oncebefore and onceafteralltests Part One Anatomy of a Test Case
  • 6. The TestCase can contain any number of public methods. Those public methods are considered tests regardless of their name, and MXUnit will run them as tests. Any private methods are not considered tests and will not be run by MXUnitFailures will be recorded as failures; errors as errors; and successes as successes Inside of your tests, you make assertions on the results of functions that you call on your component under test Part Two Anatomy of a Test Case
  • 7. To very quickly get started, you can run the test by loading it in the browser and suffixing it with "?method=runTestRemote", like so: http://localhost/tests/MyTest.cfc?method=runTestRemoteExample TestCase:Part Three Anatomy of a Test Case
  • 8. Write the test firstWatch the test failWrite the componentWatch the test passTest Driven Development allows you to write your test cases before you begin development.
  • 9. One caveat to TDD is the components you developare only as good as the tests you write. So getgood at writing tests if you want to use TDD.By Kent Beck Test Driven Development (TDD)
  • 10. The core of unit testing, i.e. Assertions, are simple: You assert something, and if that assertion does not result in your expectation, the test fails. All code AFTER a failed assertion will NOT execute. Repeat: A failed assertion means the end of that test.This is why it's best practice to not load each test function with lots of assertions. A single failed assertion means that any subsequent assertions in a test will not execute and thus you won't know if those assertions indicate further problems in your code.In other words, one assertion per test function is strongly suggestedas¡sert - to demonstrate the existence of Assertions
  • 11. assertTrue(boolean condition [,String message])assertTrue is one of the two "bread and butter" assertions in any testing framework. The philosophy is simple: you assert something to be true; if it's not, the test failsassertEquals (expected,actual[, String message])assertEquals is the other core assertion. Here, you're asserting that some actual result equals some expected result. This could be two numbers, two strings, two structs, whatever. For now, MXUnit follows the JUnit pattern of using a single assertEquals to compare any type of data.assertFalse(booleancondition [, String message]) Built-In Assertions
  • 12. assertSame (obj1,obj2 [,String message])Used to determine if the obj1 and obj2 refer to the same instance.Note that arrays in Adobe ColdFusion are passed by value, so, this will always fail for arrays.assertNotSame (obj1,obj2 [,String message])Used to determine if the obj1 and obj2 do not refer to the same instance.Note that arrays in Adobe ColdFusion are passed by value, so, this will always pass for arrays. Built-In Assertions
  • 13. assertIsTypeOf (component obj, String type)Determines if obj is of type. type needs to be fully qualified (i.e. ‘core.beans.User’).assertIsXMLDoc (any xml [, String message])Determines if xml is a valid XML DOM object.assertIsArray (any obj1)Determines if the obj1 is a valid array.assertIsDefined*(any obj1)Determines if the obj1 has been defined in the available scope.These are extensions to the base set of assertions and are specific to ColdFusion.MXUnitAssertion Extensions
  • 14. assertIsEmpty (any obj1)Determines if the obj1 is a 0 length string or NULL equivalent.assertIsEmptyArray (any obj1,[String message])Determines if the obj1 is an array with no elements.assertIsEmptyQuery(any obj1,[String message])Determines if the obj1 is a query with no rows.assertIsEmptyStruct (any obj1,[String message])Determines if the obj1 is a struct with no keys or values.MXUnitAssertion Extensions
  • 15. assertIsQuery (any q)Determines if q is a valid ColdFusion query.assertIsStruct (any obj)Determines if obj is a valid ColdFusion structure.MXUnitAssertion Extensions
  • 16. The steps for creating your custom assertion are as follows:Write a test for your assertionWrite the assertionDecide how you want to load it: Always or only on selected tests.Assertion Rules:Your assertion will need to throw mxunit.exception.AssertionFailedError or use an existing assertion that throws this exception.If you want to have optional first or last parameter message, you will need to call normalizeArguments(arguments) in your code.Provides an easy way to add custom assertions to your tests without having to change the mxunit core. Custom Assertions
  • 17. Data driven testing allows you to execute tests with a wide variety of input data. This can make creating and executing certain kinds of tests efficient and very powerful. Essentially, you provide a reference to a collection of data and MXUnit will iterate over that data and execute the test for each item in the collection.Data ProvidersArrayQueryListExcel/CSVIteratormxunit:dataprovider=“<data provider>”With MXUnitdataproviders Data Driven Testing
  • 18. TransactionsThe goal is to have a way to execute code that utilizes database logic (inserts, updates, deletes, etc.), test the results of the code, and set the state of the database back to a known state. Leveraging transactions is one way to achieve this. The process is to execute code and after assertions, roll back any transactions. Sounds simple ... it is and it isn't. ColdFusion is limited with transaction handling - you cannot have nested transactions and you need to use the <cftransaction ...> tag.A Test Adapter Pattern for DAO TestingDAO Test Adapter Pattern
  • 19. Write the test firstExtend the DAO component under test to DAOTestAdapter, or similarRefactorour DAO component: extracting the database logic to a package access method with no transaction handlingCode this packageaccessed method within the transaction block of the public DAO methodCreate a method in DAOTestAdapter that overrides the DAO method under test and calls the package access method, but rolls back the transaction no matter whatRun the test. The expected behavior is that data is inserted into the db, and the method returns trueA Test Adapter Pattern for DAO TestingDAO Test Adapter Pattern
  • 20. Resulting-State AssertionThe resulting-state assertion tests data. It says "I'm doing something to my object that will change that object's data, or 'state'. I'm going to test that the resulting state of my object is as I expect". A simple example is the common "bank account" or "transaction" example: You have two accounts, you transfer $20 from one account to another, and you test that the first account is 20 bucks shorter and the second account has that 20 bucks.Different instances, same dataThe different-instances, same-data pattern is common in DAO testing. Essentially, we're asserting that two objects are different instances but contain the same data. In MXUnit, you can test for "different instance" by using the assertNotSame() assertion.**The terms here are taken from the outstanding book "Test-Driven" by LasseKoskela. Assertion Patterns
  • 21. Guard AssertionThe guard assertion is simply a slight variant on the resulting state assertion; typically, the difference is that toward the top of the test, before you get into the "guts" of your assertion(s), you check the object for some condition that you want to ensure exists before proceeding with the meat of your tests. Think of it as "If this condition isn't true, I want to fail right now because the rest of the tests don't matter". Usually the "guard" is just a simple assertion for equality, often to check that a "default" condition exists.Interaction AssertionWith interaction assertions, we're testing to make sure an object and a collaborator worked together as we expected. A great example of an interaction is a "bean" style object, like perhaps a "User", and the DAO for that object, like a UserDAO Assertion Patterns
  • 22. "Delta" AssertionSometimes you can't assert an absolute equality (like "My list is now 5 elements long"). Sometimes, you have to assert equality relative to some previous state. Imagine you're hooking into some scheduling mechanism, for example. We don't know exactly what getTotalScheduled() will return at any given test run. Maybe it's 1. Maybe it's 30. Who knows. What we want to test is that when we schedule one additional thing, our scheduler's "totalScheduled" count increases by 1.This type of assertion, where we compare the state right before and right after performing some task, is called "delta", or difference, assertion. Assertion Patterns
  • 23. MXUnit was built to make it as easy as possible to create tests and test suites.The steps for creating and running a TestSuite are:Create a ColdFusion page to run the suiteCreate a TestSuite objectTell the TestSuite what tests to addrun() the TestSuitePrint the outputRun the suite in your web browserA TestSuite is a collection of tests that logically fit together.Creating and Running a TestSuite
  • 24. http://www.mxunit.orgTest Driven Development: By ExampleTest Driven: TDD and Acceptance TDD for Java DevelopersColdFusion 9 Developer Tutorial – Chapter 14Marc Esher’sMXUnit Blog Additional Resources