Transcript
Page 1: Test Driven Development in AEM/CQ5

TDD in CQ5/AEMSagar Sane

Page 2: Test Driven Development in AEM/CQ5

What’s on the plate?● What is Test driven development (TDD)?

● Why TDD / BDD?

● CQ backend and Spock testing framework

● Live code

Page 3: Test Driven Development in AEM/CQ5

Not on the plate● Advanced testing techniques

○ Features of Spock Framework

● Much about testing your UI / JS

Page 4: Test Driven Development in AEM/CQ5

● Software Consultant II / Tech lead

● WEM Consultant○ Backend development○ Comfortable with Java (/JSP) and Groovy

● I don’t dislike front-end development :-)

About me

Page 5: Test Driven Development in AEM/CQ5

I became a programmer because of the ‘science’ in programming,

but, I remained a programmer because of the ‘art’ in programming.

I believe..

Page 6: Test Driven Development in AEM/CQ5

Some people have very strong opinions about this...

Page 7: Test Driven Development in AEM/CQ5

No, REALLY!

Page 8: Test Driven Development in AEM/CQ5

Test Driven Development● Write Test First (extreme programming)

○ Technical meaning○ Design your code around the tests

● Write a lot of tests ○ Milder approach

Page 9: Test Driven Development in AEM/CQ5

What is a Unit Test in CQ/AEM?

Page 10: Test Driven Development in AEM/CQ5

Benefits of Testing● Find bugs early

● Make it easy for a new person on your project○ Read the tests if you don’t understand the code

● Improve code quality

Page 11: Test Driven Development in AEM/CQ5

Benefits of Testing● Helps during code re-factoring

● A good test is worth a thousand comments

● Executable documentation

Page 12: Test Driven Development in AEM/CQ5

BDD● Behavior Driven Development

● Writing tests from the customer’s / stakeholder’s point of view

● It really is TDD done well!

● More expressive than TDD

Page 13: Test Driven Development in AEM/CQ5

//Variablesexpected, v1, v2, ..

//Execute featureactual = System.executeFeature(v1, v2, ..)

//The system gives output as expectedassert actual == expected

Page 14: Test Driven Development in AEM/CQ5
Page 15: Test Driven Development in AEM/CQ5

CQ/AEM and Spock● What is Spock Framework? [ link ]

● Why is it awesome? [ link ] [ link ]○ Is beautiful

● It can be used to test your CQ/AEM backend!

Page 16: Test Driven Development in AEM/CQ5

A basic Spock Feature

//Spock ‘feature’def "My component should just work"() { given: final variables = [ //initial conditions ] final expected = new MyComponent(...)

when: final actual = MyComponent.create(variables)

then: actual == expected}

Page 17: Test Driven Development in AEM/CQ5

Time for some real code...What ‘new’ will we see?

● Mocks in spock

● setupSpec(), cleanupSpec()

● @Ignore(), @Shared

Page 18: Test Driven Development in AEM/CQ5

What all can you do?

● Unit tests for servlets, services

● Integration tests

● Functional tests for your rest APIs

● Even UI tests! (with Geb)

Page 19: Test Driven Development in AEM/CQ5

What all can you do?

Test Automation

Page 20: Test Driven Development in AEM/CQ5

Where to go from here?

● Don’t have to start big

● Add tests to the code that is important, needs change

● Start new implementations with TDD

Page 21: Test Driven Development in AEM/CQ5

The art of programming!

Page 22: Test Driven Development in AEM/CQ5

Thank you!