In my last post, I explored three different ways to use Groovy in our AEM projects. Using Groovy to build OSGi components, servlets and models seemed the most promising. Now I bring you a fourth one that I find as promising. Unit testing with Groovy & Spock.
There are a lot of topics here. TDD vs. BDD. JUnit vs. Spock. Groovy vs. Java. My goal here is not to give you a complete analysis. That's up to you based on your team composition and client. I am going to give you some context. Then I will show you is how to set it up in an AEM project and show you a few examples.
TDD vs. BDD
You can spend some time researching Test Driven Development and Behavior Driven Development. To put it simply
TDD is for a small technical audience, such as developers. To test small pieces of code. In AEM this could be a Java Sling model or OSGi service.
Since TDD focuses more on what the code is doing, it follows the Arrange-Act-Assert pattern. And since BDD focuses on describing the user action, it follows the Given-When-Then pattern. TDD uses AAA to describe the same thing BDD does with GWT. They are just idioms for the three phases of a test
Given/Arrange: Set stuff up and get all my ducks in a row.
When/Act: Run the thing I want to test.
Then/Assert: Check that the things I expected actually occurred.
If you have been writing unit tests in AEM, you have been doing TDD. It makes sense because we are testing small units of code. The tests get written by developers. BDD makes more sense for integration testing. A product owner provides acceptance criteria. Which then a tester uses to create automated tests. With Selenium and Cucumber for example.