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..
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
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.
15. 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!
16. 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
}
17. Time for some real code...
What ‘new’ will we see?
● Mocks in spock
● setupSpec(), cleanupSpec()
● @Ignore(), @Shared
18. What all can you do?
● Unit tests for servlets, services
● Integration tests
● Functional tests for your rest APIs
● Even UI tests! (with Geb)
Hello everyone…
Thanks for joining, today we will be talking about Test Driven Development.
- 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!
.. 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
We will not talk about these
I am this.. that..
PAUSE...
So, here is a statement I read somewhere recently
I am really starting to believe in the above saying.
-- I want to be able to say this for myself in say 10 years for now.
But, What does this mean? art? what is the ‘art’ of programming?
-- We will try to answer these questions by the time we end the talk today
… 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.
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
So, what is test driven development?
* Different if you ask different people, can mean a lot of things!
- Write tests first … have them fail and then implement features to pass them
- Write a lot of tests … ‘test oriented development’ .. this is what I like
- Umm people might not always be on-board to this thought
- It is all about testing a small ‘unit’ functionality of code.
- In terms of CQ, it can be anything ..
** from testing the ‘activate()’ method of your service ..
** to testing if your servlet responds to a ‘doPost()’ call to ..
** testing if your factory method on a component WORKS
- We will be looking at each of these pieces live in action when we get to the ‘looking at the code’ section.
….. QUESTIONS ??
Why is it useful?
- 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.
- HUGE CODEbase.. new person.. enterprise application codebase. Read tests if you don’t understand the code
- 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.
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.
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
- Is about Implementing application by describing an application from the perspective of the stakeholders
- A new feature requested by the stakeholders is not delivered until it has tests (Code Coverage)
- More expressive, readable for humans, than just for computers to execute
- Tests whose names are real sentences
Lets take an example of a simple ‘pseudotest’ .. or a ‘usecase’ for a test.
We want to assert that a feature in a system works as expected!
If we want to take the TDD or BDD approach
* Very natural to read
* easy to understand what its doing .. etc.
So.. enter Spock framework!
- Enterprise ready testing framework for testing Java and Grooy application
- What makes it stand out from the crowd is its highly expressive specification language.
- Next generation testing framework!
- Spock lets you write specifications that describe expected features (properties, aspects) exhibited by a system of interest.
- Why is it awesome?
-- Its beautiful.. expressive, based on rspec, junit, etc.
-- Easy to learn, powered by Groovy, which is basically java on steroids!
-- Several Spock Extensions for like Spring Framework, etc. and it also provides apis to write custom extensions
...QUESTIONS ??
So lets take a simple example of how testing in Spock can look like.
Now.. lets take an example of how you can write a simple spock unit test (spec) to test the backend of your component JSP.
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.
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.
setupSpec, cleanupSpec - Runs before 1st feature method, runs after the last feature method
ignore - Skipping tests, shared - shared between feature methods / tests
…… QUESTIONS??
--- All done using spock.. can be extremely readable and can help understand your system’s usecases better.
--- 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.