TDD — 5 IDE Helpers to Make Development Easier
When we are developing software, most of the time we are not typing. Reading and thinking take the largest share. Nevertheless it makes sense to optimise the typing part. Because we want quick feedback if we have an idea. This is especially true in the red-green-refactor cycle of TDD.
So, let’s have a look at what is offered by an IDE that could help us. Here we use IntelliJ (and Java) but every proper IDE should provide similar functionalities.
1) Creating Tests
First, we have a look at creating tests, because this is the starting point in TDD. ⌘ + n (Mac) or Alt + insert (Linux, Windows) are our friends here and offer us a generator for “Test Method”:
It let’s us directly type the test name and gives us the skeleton for the test method:
Similarly SetUp or TearDown methods can be created.
2) Creating Classes
TDD is not only about testing but also designing a system. We think of a unit (here class) and want to get it into our test quickly to see how it looks like. This helps us to think about the interfaces and parameters. In our example we want to create a shopping cart and our idea is to have something like a factory to do that:
So we “designed” a ShoppingCartFactory in our test and thought it would be a good idea if this class has a static method create() since we want to create a shopping cart in the end. Although it’s red, it looks good to us. As a client of this class we are happy. So, let’s get rid of the red color. Alt + enter on the red word gives us a little dialog with several options:
Let’s choose the first option “Create Class” and we are one step further:
3) Creating methods on classes
As soon as we have classes we can decide about which behaviour the class should offer. We went for create and use Alt + Enter again to add this method into the ShoppingCartFactory class:
IntelliJ knows that this has to be a static method. And it allows us to define a return type too:
In the end we want the method to return a shopping cart. Hence, we go for a ShoppingCart type. The null return is added automatically:
4) Introduce local variable
Another Alt + Enter → Create Class on the red word above gave us a ShoppingCart class. Our test is green but it does not do much yet:
We want to see and check what we are getting back from this create method. Let’s introduce a local variable for the result (Alt + Enter on the expression):
This also gives us the type for the variable (ShoppingCart) and we can directly declare it final:
We start with a simple assertion that gives us quick feedback:
For the next step, let’s assume we have some classes in place. Also for that generators can help. I added fields to the class ShoppingCart and created Customer and Item in a similar way:
5) Add as parameter
On the ShoppingCart we have a Customer. It’s probably a good idea if the ShoppingCartFactory adds this customer to the object during creation. That means create should probably have a parameter of type Customer. So, let’s continue designing from the test’s perspective:
Local variable by using Alt + Enter on customer:
As a client of ShoppingCartFactory we think that the object should offer us the possibility to pass a customer. Here again Alt + enter helps us with another dialog:
The customer is added as a parameter after choosing the first option above:
Looking from a test’s perspective (client’s perspective) onto our modules and classes helps us to design our components “user-friendly”. Most IDEs offer shortcuts to get your ideas into running code quickly. Like that you can concentrate on the more difficult tasks, e.g. thinking about the design of your code.
Could this be your next step?
Or should the interface be different?