Test Fast!
For test-driven development, integrated development environments like Eclipse provide shortcuts for use of tools like JUnit. The Eclipse Test Fast video illustrates the use of some of these.
Test-first development leads to greater testing, which leads to higher productivity. Test-first development, however, requires different ways of working, one that can seem “backwards” at first.
Many programmers use autocomplete, for example using Ctrl+Space in Eclipse (followed by the down arrow and enter to select an option). If you write the test first, autocompletion may not be available for the system under test. The video illustrates using Eclipse Quick Fix with the most backwards approach: the Assert First pattern (see page 128 of Test-Driven Development: By Example) using the Money example (see page 12). For each visual clue, there is a shortcut key to accomplish the next step:
- When the wavy red line appears, move the cursor to the code, press Ctrl+1 (followed by the down arrow and enter) to select the appropriate Quick Fix. (You can also hover the mouse pointer over the code and then select the fix with the mouse pointer.)
- When keywords and identifiers have a box outline, press tab to move between them and enter to return to the original location.
- When an asterisk appears before the file name, press Ctrl+S (or the “Save” button) to save the file and update incremental compilation.
- When a Quick Fix results in a different window in the foreground, press Ctrl-F6 to switch back (or select the appropriate tab with the mouse).
- When the test has no markers in the left editor margin, press Alt+Shift+X for the “Run” menu, and ‘T’ for “Run JUnit Test.” (You can also right-click and then select Run As…->JUnit Test.)
These shortcuts enable constructing the code backwards. The following are the first seven steps:
- Start by typing the test method:
@Test public void testMultiplication() { } - Use Quick Fix to automatically import “Test”:
import org.junit.Test;
- Assert first:
assertEquals(15, product.amount);
- Use Quick Fix to create local variable “product” and tab to
Object
and replace with the system under test,Dollar:Dollar product;
- Use Quick Fix to create class
Dollar:package org.agileiq; public class Dollar { } - Return to
DollarTest(Ctrl-F6) and use Quick Fix to create fieldamountin typeDollar. Use tab and typeintover the defaultObject. (Yes, encapsulate this public field later to make it private.)public int amount;
- Save
Dollar(Ctrl-S), return toDollarTest(Ctrl-F6), and go to the top of the file (Ctrl+Home) to insert the static import:import static junit.framework.Assert.assertEquals;
Note that Quick Fix isn’t available with static imports, but autocomplete is (type “ju”, Ctrl+Space, down arrow to framework, enter, “.As”, Ctrl+Space, Ctrl+Space, enter).
The remainder of the video uses the same techniques to complete a failing test.
A simple Eclipse sample not shown in the video isĀ right-clicking on a class (e.g., “Dollar”) and selecting New->JUnit Test Case. Eclipse proposes a test case named after the class you selected (e.g., “DollarTest”), with the original class as the class under test.

When you select “Next,” Eclipse offers the available methods to test.

If you use this shortcut, I won’t tell anyone that you’re not using test-first development.
What are you favorite Eclipse shortcuts?
