Quick revision
The short answer
@Sql runs SQL scripts before or after a test; it is simple and fast but ties tests to table and column details and hides the data the test depends on in another file. A test data builder creates objects with sensible defaults and lets each test override only what matters, so the important values stay visible in the test. An Object Mother hands out named, ready-made objects such as aPremiumCustomer(); it is convenient but tends to grow into a large shared class, so many teams combine it with builders.
What to remember
In this answer, we compared three ways to set up test data. @Sql scripts are simple and right for SQL-centred tests, but they couple tests to schema details and hide the important values in another file. Test data builders give valid defaults and let each test state only what matters, which keeps tests readable and limits schema changes to one place. An Object Mother gives named scenarios and a shared vocabulary but tends to grow into a large shared class. Combining them, with Object Mother methods that return builders, gets most of the benefits of each.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
@Sql runs SQL scripts before or after a test; it is simple and fast but ties tests to table and column details and hides the data the test depends on in another file. A test data builder creates objects with sensible defaults and lets each test override only what matters, so the important values stay visible in the test. An Object Mother hands out named, ready-made objects such as aPremiumCustomer(); it is convenient but tends to grow into a large shared class, so many teams combine it with builders.
Why this question is asked
Every integration test needs data, and how we create that data decides whether the test is easy to read and cheap to change. When a test fails, the first question is 'what was in the database?', and the answer should be on the screen, not three files away. Interviewers ask this comparison because it shows whether we have maintained a large test suite and felt the pain of each approach. In this answer, we will look at each of the three approaches with an example, see what each costs as the schema and the suite grow, and then see how they combine in practice.
1@Sql: Running Scripts Around a Test
@Sql is a Spring test annotation that runs one or more SQL scripts against the test data source. By default the scripts run before the test method, and with executionPhase = AFTER_TEST_METHOD they run after it, which is handy for cleanup. In a transactional test, the scripts run inside the test transaction, so they are rolled back with it.
The strengths are real. Scripts are quick to write, they can load data that would be awkward to build through the code, and they are the natural choice for testing a native query against a precise set of rows. But notice the cost in the test above. The expected value 59.90 depends on numbers that live in a different file, so the reader must open the script to understand the test. The script also names tables and columns directly. When a migration renames tier to plan or adds a non-null column, every script that inserts into that table breaks, and in a large suite that can be dozens of files. Scripts also get shared between tests, and a change made for one test silently changes the data of another.
2Test Data Builders: Defaults Plus What Matters
A test data builder is a small class that creates a valid object with sensible defaults, and lets the test change only the fields it cares about. Everything the test does not mention gets a reasonable value, so the object is always valid.
Now the test shows exactly the values that produce 59.90, and nothing else. The customer, the status and any other fields come from defaults that no reader needs to think about. When the schema changes, we fix the builder once and every test that uses it keeps working. And because the builder goes through the entity classes and the repository, it also exercises the real mapping. The cost is that someone has to write and maintain the builders, and a builder with poorly chosen defaults can hide data that does matter.
3Object Mother: Named, Ready-Made Objects
An Object Mother is a class of static factory methods that return complete objects for well-known situations, each with a name from the domain.
The name premiumCustomer() reads well and gives the team a shared vocabulary. The trouble starts as the suite grows. Each new test needs a slightly different object, so someone adds another method, and the class grows to hundreds of methods with names like premiumCustomer2(). Because many tests share the same method, changing it to suit one test breaks others, so people stop changing it and add yet another variant. And the values that matter are hidden inside the factory, just as with @Sql.
4Comparing the Three
Seen side by side, the three approaches trade convenience against visibility and robustness.
| Aspect | @Sql scripts | Test data builders | Object Mother |
|---|---|---|---|
| Where the data is visible | In a separate SQL file | In the test itself | Inside a factory method |
| Coupling to the schema | High: tables and columns | Low: one builder to fix | Low: one factory to fix |
| Effort to start | Very low | Medium: write builders | Low |
| Risk as the suite grows | Shared scripts change silently | Poor defaults hide data | Huge shared class |
| Best for | Native queries, bulk data | Most integration tests | Common named scenarios |
The approach most teams settle on is a combination: builders as the base, with an Object Mother whose methods return pre-configured builders rather than finished objects. A test can write aPremiumCustomer().withCountry(FR).build(), getting the shared vocabulary of the Object Mother and the visible overrides of the builder. @Sql then stays for the cases where SQL is the point, such as native query tests or bulk data for a report, kept small and next to the test that uses it.
5Follow-up questions
Should builders save the entity or just build it?
I prefer builders that only build, so they can be used in plain unit tests too, and the test saves the object through the repository when it needs a database. Some teams add a helper that builds and saves in one step for integration tests, which is fine as long as the plain build() stays available.
How do builders relate to Java records?
Records are immutable and have only a canonical constructor, so a builder is still useful for test data when a record has many fields. The builder holds defaults and calls the record's constructor in build(). For records with two or three fields, calling the constructor directly is usually clear enough.
Where does reference data such as countries or currencies come from?
From the migrations, not from test scripts or builders. Data that every environment needs, such as a list of countries, belongs in Flyway or Liquibase migrations, so tests and production share it. Builders then refer to that existing reference data instead of creating it.
6Conclusion
In this answer, we compared three ways to set up test data. @Sql scripts are simple and right for SQL-centred tests, but they couple tests to schema details and hide the important values in another file. Test data builders give valid defaults and let each test state only what matters, which keeps tests readable and limits schema changes to one place. An Object Mother gives named scenarios and a shared vocabulary but tends to grow into a large shared class. Combining them, with Object Mother methods that return builders, gets most of the benefits of each.
Open with
@Sql runs SQL scripts before or after a test method, inside the test transaction if there is one. It is quick to write and it is the right tool when the SQL itself is the point, like testing a native query against exact rows.
Then, if they want more
The downsides are that the test depends on data in a separate file, so the reader cannot see why the expected value is what it is, and scripts name tables and columns directly, so every schema change breaks them, and shared scripts change other tests' data silently.
A test data builder creates a valid object with sensible defaults and lets the test override only what matters, like an order with two specific lines. The important values are visible in the test, and a schema change is fixed once in the builder.
An Object Mother gives named ready-made objects, like premiumCustomer, which reads nicely and gives a shared vocabulary, but over time it grows into a big shared class with many near-duplicate methods, and changing one breaks other tests.
In practice I use builders as the base and let Object Mother methods return pre-configured builders, so I can write a premium customer with a different country in one line. I keep @Sql for SQL-focused tests, small and next to the test, and reference data comes from migrations.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Compare @Sql scripts, test data builders and the Object Mother pattern for setting up...": https://scrivio-interview-library.netlify.app/java/integration-testing/b5d03faa52/