• Save
Test Driven Development in CQ5/AEM
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Test Driven Development in CQ5/AEM

  • 411 views
Uploaded on

In this talk, we will go over what TDD is? And what does it mean for CQ5/AEM development (in this case, primarily AEM's Backend architecture)

In this talk, we will go over what TDD is? And what does it mean for CQ5/AEM development (in this case, primarily AEM's Backend architecture)

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
411
On Slideshare
374
From Embeds
37
Number of Embeds
3

Actions

Shares
Downloads
1
Comments
0
Likes
2

Embeds 37

https://twitter.com 17
http://www.slideee.com 16
https://www.linkedin.com 4

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Hello everyone… <br /> <br /> Thanks for joining, today we will be talking about Test Driven Development. <br /> <br /> - Testing during development.
  • We will discuss about Test Driven Development , Behavior Driven Development. What it is, what are the benefits? Then, try apply our understanding of TDD / BDD to testing our backend code in CQ using Spock Framework ) .. and also look at some code! <br /> <br />
  • .. We will NOT be going into advanced details .. my goal is to get people excited about testing. Hopefully, the testing framework I will be talking about (Spock) will help. We will also be looking at some live code later, going through some basics examples of Spock testing <br /> <br /> We will not talk about these
  • I am this.. that.. <br /> <br /> <br /> PAUSE...
  • So, here is a statement I read somewhere recently <br /> <br /> I am really starting to believe in the above saying. <br /> -- I want to be able to say this for myself in say 10 years for now. <br /> But, What does this mean? art? what is the ‘art’ of programming? <br /> <br /> -- We will try to answer these questions by the time we end the talk today <br /> <br /> <br /> … having said that.. lets get started!
  • Some people have very strong views about testing. They just have to have tests no matter what!
  • No, they really may be intimidating. <br /> <br /> But, what I want to do here is to NOT try and enforce my opinion, but lay down MY thoughts about testing, what it means to me, how it can help, etc.
  • I believe in a bit milder approach <br /> <br /> So, what is test driven development? <br /> <br /> * Different if you ask different people, can mean a lot of things! <br /> <br /> - Write tests first … have them fail and then implement features to pass them <br /> - Write a lot of tests … ‘test oriented development’ .. this is what I like <br /> - Umm people might not always be on-board to this thought
  • - It is all about testing a small ‘unit’ functionality of code. <br /> <br /> - In terms of CQ, it can be anything .. <br /> ** from testing the ‘activate()’ method of your service .. <br /> ** to testing if your servlet responds to a ‘doPost()’ call to .. <br /> ** testing if your factory method on a component WORKS <br /> <br /> - We will be looking at each of these pieces live in action when we get to the ‘looking at the code’ section. <br /> <br /> ….. QUESTIONS ?? <br />
  • Why is it useful? <br /> <br /> - Helps to find bugs early. If you have important piece of code and you have tests backing it, over time, its going to be invariably easier to find bugs early during development. <br /> <br /> - HUGE CODEbase.. new person.. enterprise application codebase. Read tests if you don’t understand the code <br /> <br /> - And also, if you want to write good tests, you eventually try to make your code sound . Meaning, you use the correct programming patterns. So.. you also get the opportunity to receive kudos! :)
  • Another one is .. re-factoring. <br /> <br /> Say eg: YOu have a code which has not been touched for a LONG time. You know.. its like a dark room in your grandma’s house. If you have tests for that implementation, then you are in relatively good shape. Even if I mess things up in my refactoring, you know that that tests can save you. <br /> <br /> Lastly, a good test is worth a thousand words! This is true .. and you will see it in action soon.
  • Dan North - Coined the name BDD <br /> <br /> - Is about Implementing application by describing an application from the perspective of the stakeholders <br /> <br /> - A new feature requested by the stakeholders is not delivered until it has tests (Code Coverage) <br /> <br /> - More expressive, readable for humans, than just for computers to execute <br /> <br /> - Tests whose names are real sentences
  • Lets take an example of a simple ‘pseudotest’ .. or a ‘usecase’ for a test. <br /> <br /> We want to assert that a feature in a system works as expected! <br /> <br />
  • If we want to take the TDD or BDD approach <br /> * Very natural to read <br /> * easy to understand what its doing .. etc. <br /> <br /> So.. enter Spock framework!
  • - Enterprise ready testing framework for testing Java and Grooy application <br /> - What makes it stand out from the crowd is its highly expressive specification language. <br /> - Next generation testing framework! <br /> - Spock lets you write specifications that describe expected features (properties, aspects) exhibited by a system of interest. <br /> <br /> - Why is it awesome? <br /> -- Its beautiful.. expressive, based on rspec, junit, etc. <br /> -- Easy to learn, powered by Groovy, which is basically java on steroids! <br /> -- Several Spock Extensions for like Spring Framework, etc. and it also provides apis to write custom extensions <br /> <br /> ...QUESTIONS ??
  • So lets take a simple example of how testing in Spock can look like. <br /> <br /> Now.. lets take an example of how you can write a simple spock unit test (spec) to test the backend of your component JSP. <br /> <br /> Every test in Spock is really a ‘feature’, as we saw with regards to BDD, we want to test a feature or a behavior of the system in question. <br /> <br />
  • Mocks - A mock is an object of a certain type but without any behavior. Instead, the expected behavior is specified by the developer writing the specification. <br /> <br /> setupSpec, cleanupSpec - Runs before 1st feature method, runs after the last feature method <br /> <br /> ignore - Skipping tests, shared - shared between feature methods / tests <br /> <br /> <br /> …… QUESTIONS??
  • --- All done using spock.. can be extremely readable and can help understand your system’s usecases better. <br /> --- Integration tests for example.. when done in Spock..
  • It is not just enough to have a lot of tests. I think what will provide real value of the tests, is continuous delivery and integration. So using tools like Jenkins CI, you can basically use these tests as means to catch errors before they go to production, or to user testing.
  • SUMMARIZING
  • So answering the question that we asked at the start. The art of programming really lies in being a good programmer that can write code that humans can understand. I believe that having the mindset of writing tests, is going to help you achieve that purpose. <br />
  • … and that’s it.

Transcript

  • 1. TDD in CQ5/AEM Sagar Sane
  • 2. What’s on the plate? ● What is Test driven development (TDD)? ● Why TDD / BDD? ● CQ backend and Spock testing framework ● Live code
  • 3. Not on the plate ● Advanced testing techniques o Features of Spock Framework ● Much about testing your UI / JS
  • 4. ● Software Consultant II / Tech lead ● WEM Consultant o Backend development o Comfortable with Java (/JSP) and Groovy ● I don’t dislike front-end development :-) About me
  • 5. I became a programmer because of the ‘science’ in programming, but, I remained a programmer because of the ‘art’ in programming. I believe..
  • 6. Some people have very strong opinions about this...
  • 7. No, REALLY!
  • 8. Test Driven Development ● Write Test First (extreme programming) o Technical meaning o Design your code around the tests ● Write a lot of tests o Milder approach
  • 9. What is a Unit Test in CQ/AEM?
  • 10. Benefits of Testing ● Find bugs early ● Make it easy for a new person on your project o Read the tests if you don’t understand the code ● Improve code quality
  • 11. Benefits of Testing ● Helps during code re-factoring ● A good test is worth a thousand comments ● Executable documentation
  • 12. 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
  • 13. //Variables expected, v1, v2, .. //Execute feature actual = System.executeFeature(v1, v2, ..) //The system gives output as expected assert actual == expected
  • 14. CQ/AEM and Spock ● What is Spock Framework? [ link ] ● Why is it awesome? [ link ] [ link ] o Is beautiful ● It can be used to test your CQ/AEM backend!
  • 15. 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 }
  • 16. Time for some real code... What ‘new’ will we see? ● Mocks in spock ● setupSpec(), cleanupSpec() ● @Ignore(), @Shared
  • 17. What all can you do? ● Unit tests for servlets, services ● Integration tests ● Functional tests for your rest APIs ● Even UI tests! (with Geb)
  • 18. What all can you do? Test Automation
  • 19. 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
  • 20. The art of programming!
  • 21. Thank you!