Start from the end

Recommended reading before you read this article: Fest assertions.

Lets assume we want to create a calculator. That calculator will be able to add numbers, so the result of 3+4 should be 7. Please notice that we just expressed an expectation from our calculator.

In most cases I found that starting writing tests from the end is the best way to go. Start from what you expect.

For example:

Stage 1, specify the output, start with the expectation (will not compile)

Stage 2, express what actually has to happen to get to the expectation

Stage 3, make the test pass.

Try to always start with writing one or more assertThat() or verify(). Then proceed to the other parts of the test.This forces you to think about what you actually want to achieve rather then concentrate on implementation and other distractions. I know this example might be too simple to see the benefits clearly unfortunately. It was just to demonstrate the process. Try to implement this process in your projects and you will see the benefits.