FIRST is a short checklist for writing unit tests that we can trust and run often. It stands for Fast, Independent, Repeatable, Self-validating and Timely. Each word describes a practical quality of a useful test, from how quickly it runs to whether it can decide its own result.
A unit test checks a small piece of application behavior. For example, it might check whether a delivery fee becomes zero when an order reaches the free-delivery threshold. We want that test to give us a clear answer without requiring a running website or a manually prepared database.
1. The FIRST Checklist
Some descriptions use “Isolated” for I and “Thorough” for T. In this guide, independent means that tests do not rely on each other’s state. Timely means that we write tests while we are working on the behavior, when the requirements are still clear. Thorough coverage of useful cases matters too, regardless of the spelling of the acronym.
| Principle | Question to ask during a review |
|---|---|
| Fast | Can we run the relevant tests frequently while editing the code? |
| Independent | Does it pass alone and in any order without changing another test’s result? |
| Repeatable | Does the same code with the same inputs give the same result? |
| Self-validating | Does an assertion decide whether the behavior is correct? |
| Timely | Have we written the test while implementing or changing the behavior? |
2. Fast Tests Give Us Feedback While We Work
A test that takes a little time on its own can become expensive when repeated across a suite. If 2,000 tests each take 200 milliseconds and run sequentially, they take 400 seconds, or 6 minutes and 40 seconds, before other build overhead.
We first look at where the time goes. A calculation test rarely needs a Spring application context, a network request or a database connection. Constructing the class directly and providing its inputs can remove unnecessary setup.
We can replace an external dependency with a test double, an object that provides controlled behavior for the test. A stub returns a prepared response. A mock can also check an interaction, such as whether a notification was sent. We use them when they help test our code, rather than mocking every object automatically.
Fast unit tests do not replace database or API integration tests. Those tests check whether components work together and may take longer. We keep their purpose visible instead of weakening them just to make every test run in milliseconds.
3. Independent Tests Prepare Their Own State
A test that updates an employee should not require another test to create that employee first. When we run only the update test, it still needs all its prerequisites.
We create fresh mutable data for each test. In JUnit Jupiter, \@BeforeEach can prepare shared setup before every test method. A small amount of setup inside the test is often clearer when only that test needs it.
Shared, unchanging values are usually fine. A shared mutable list, however, can leave data behind for the next test. Clearing it afterward may help, but avoiding the shared list entirely usually makes the tests easier to understand.
Independent tests also make failures easier to investigate. They do not guarantee that debugging is unnecessary, but they remove the need to reconstruct an earlier test’s actions.
4. Repeatable Tests Control Changing Inputs
Time, random values, machine settings and remote services can change between runs. If a test relies on today’s date, it may pass today and fail next month without any code change.
Java’s Clock lets us supply the time used by date and time operations. The following example tests a subscription’s expiration rule. The subscription is expired when the current instant reaches its end time. The snippets belong in the same package in a project with JUnit Jupiter configured for tests.
import java.time.Clock;
import java.time.Instant;
final class Subscription {
private final Instant endsAt;
Subscription(Instant endsAt) {
this.endsAt = endsAt;
}
boolean isExpired(Clock clock) {
return !Instant.now(clock).isBefore(endsAt);
}
}
The test fixes time at the exact expiration instant. It does not wait for real time to pass.
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import org.junit.jupiter.api.Test;
class SubscriptionTest {
@Test
void expiresAtItsEndTime() {
Instant end = Instant.parse("2026-01-01T00:00:00Z");
Subscription subscription = new Subscription(end);
Clock clock = Clock.fixed(end, ZoneOffset.UTC);
boolean expired = subscription.isExpired(clock);
assertTrue(expired);
}
}
This checks the equality boundary, where mistakes are easy to make. ZoneOffset.UTC supplies the fixed clock’s time zone. The expiration comparison uses instants, so the time zone does not change which moment is being compared. Separate cases can check just before and just after expiration.
For file tests, we use a temporary directory instead of a path on one developer’s machine. For database tests, we create known records in an isolated test database. The DAO testing guide explains why the database choice matters.
5. Self-validating Tests Check the Result
Printing expired and reading the console is a manual check. assertTrue(expired) lets the test runner report a failure automatically when the result is false.
An assertion should check the promised behavior. If a search must return two particular products, checking only that the result is non-null does not establish that the search is correct. We check the relevant products and, where required, their order.
The JUnit assertion guide explains assertions for values, collections and expected exceptions. We choose the assertion that expresses the requirement clearly. Several related assertions can describe one outcome; FIRST does not require exactly one assertion per test.
6. Timely Tests Record the Requirement While It Is Clear
We write a test when we add a feature, fix a bug or change a rule. At that point, we know what should happen and which cases matter. Waiting until much later makes it easier to forget why the code behaves as it does.
Test-driven development takes this further by writing a failing test before implementing the behavior. A team can also write tests alongside the implementation. In either approach, a bug fix is a good opportunity to add a test that reproduces the failure and protects the corrected behavior.
Coverage reports help us find code that no test executes, but a percentage cannot tell us whether our assertions are useful. We still need to check normal cases, boundaries and failure cases that matter to the application.
7. Conclusion
FIRST gives us five questions to ask about our tests. We keep them quick enough to run often, independent of each other, consistent across runs, able to check their own results and close to the work they describe. The unit testing best practices build on these ideas with more Java examples.
Happy Learning!
The original write up is at agileinaflash.com, and the early official PragProg article is at https://medium.com/pragmatic-programmers/unit-tests-are-first-fast-isolated-repeatable-self-verifying-and-timely-a83e8070698e
As one of the originists of the idea and one of the original article’s authors, the T is intended to refer to writing a test *just* before the code that makes it pass.
We recognized from the first day that “test-first” is controversial, and that’s okay with us. :-)
I have found the article very interesting. Thank you!