Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

Review this test and name its smells: @Test

Java
void test1() {
OrderService s = new OrderService(repo, clock);
Order o = s.create(req);
assertNotNull(o);
verify(repo).save(any());
Thread.sleep(500);
assertTrue(s.findAll().size() > 0);
}

This test has five smells. The name test1 says nothing about the behaviour; the assertions assertNotNull and size() > 0 do not check the outcome; verify(repo).save(any()) tests an implementation detail instead of a result; Thread.sleep(500) makes it slow and flaky; and it checks several behaviours at once. The fix is to split it into focused tests with descriptive names and an arrange, act, assert structure, assert specific fields and values, and use the injected clock or Awaitility instead of sleeping.

Why this question is asked

Code review of tests is where a lot of test quality is won or lost, and this kind of test is very common: it passes, it raises coverage, and it would let most bugs through. Interviewers show a short test like this to see whether we can explain why each part is weak and what to write instead. In this answer, we will read the test as it is, go through each smell and why it matters, and then rewrite it as two focused tests.

1The Test as Written

Here is the test, formatted so we can talk about each line.

Java
@Test
void test1() {
OrderService s = new OrderService(repo, clock);
Order o = s.create(req);
assertNotNull(o); // passes for any non-null order
verify(repo).save(any()); // passes if save was called with anything
Thread.sleep(500); // waits half a second, every run
assertTrue(s.findAll().size() > 0); // passes if any order exists at all
}

Notice that every assertion would still pass if create() saved an order with the wrong status, the wrong total or the wrong customer. The test exercises the code, and it would show up as covered in a coverage report, but it checks very little. Let us go through the smells one at a time.

2Smell 1: A Name That Says Nothing

test1 tells a reader nothing about what behaviour is expected. When it fails in CI at two in the morning, the report says test1 failed, and someone has to read the code to understand what broke. A test name is the first line of documentation for the rule it protects.

Good names describe the behaviour and its condition, for example createsOrderWithPendingStatus or createdOrderAppearsInFindAll. JUnit's @DisplayName can add a sentence if a method name gets too long. A useful check is whether the list of test names in a class reads like a specification of the class.

3Smell 2: Weak Assertions

assertNotNull(o) only proves that create() returned something. assertTrue(s.findAll().size() > 0) only proves that at least one order exists, which might be true even if a different test created it, or if create() stored a completely wrong order. When assertTrue fails it also gives a poor message: expected true but was false, with no values.

A strong assertion checks the specific outcome the behaviour promises: the status is PENDING, the total equals the sum of the lines, the customer id matches the request, the creation time equals the clock's time. AssertJ makes this readable and gives clear failure messages showing the actual value.

4Smell 3: Verifying an Implementation Detail

verify(repo).save(any()) checks that a particular method on a collaborator was called with any argument. It does not check what was saved, so a wrong order passes. Worse, it ties the test to how the service is implemented. If someone later changes the service to call saveAll(), or to save through a different repository, the behaviour is the same but the test breaks.

The better approach is to check the result through the public behaviour: create an order, then read it back and check its fields. If repo is a mock, we can capture the argument with an ArgumentCaptor and assert on it, which at least checks the content. Better still is a simple in-memory fake repository, so the test can check what is stored without caring which method stored it.

5Smell 4: Thread.sleep and Smell 5: Several Behaviours

Thread.sleep(500) adds half a second to every run. Across a suite with 200 such tests that is more than a minute and a half of doing nothing. It is also flaky: if the thing being waited for takes 600 milliseconds on a busy CI machine, the test fails, and nobody can reproduce it locally. If the code depends on time, the test should control time through the injected clock, which this service already receives. If the code is genuinely asynchronous, Awaitility waits until a condition is true, polling every 100 milliseconds by default, and fails with a clear message after a timeout.

Finally, the test checks creation, persistence and listing all at once. When it fails, we cannot tell which behaviour broke without debugging. Each test should check one behaviour, arranged in three visible parts: arrange the inputs, act once, assert the outcome.

The smells and their fixes
SmellWhy it hurtsFix
test1 as a nameFailure reports explain nothingName the behaviour: createsOrderWithPendingStatus
assertNotNull, size() > 0Wrong results still passAssert specific fields and values
verify(repo).save(any())Tests how, not what; breaks on refactoringCheck the stored result, or use a fake
Thread.sleep(500)Slow and flakyFixed Clock, or Awaitility for async
Several behavioursUnclear failuresOne behaviour per test, arrange-act-assert

6Rewriting the Test

Here is the same intent as two focused tests. The repository is an in-memory fake and the clock is fixed, so nothing needs to wait.

Java
class OrderServiceTest {
Clock clock = Clock.fixed(Instant.parse("2026-03-01T10:00:00Z"), ZoneOffset.UTC);
InMemoryOrderRepository repo = new InMemoryOrderRepository();
OrderService service = new OrderService(repo, clock);
@Test
void createsOrderWithPendingStatus() {
var req = new CreateOrderRequest("cust-7", List.of(line("A", 2, "10.00"))); // arrange
Order order = service.create(req); // act
assertThat(order.status()).isEqualTo(OrderStatus.PENDING); // assert
assertThat(order.customerId()).isEqualTo("cust-7");
assertThat(order.total()).isEqualByComparingTo("20.00");
assertThat(order.createdAt()).isEqualTo(Instant.parse("2026-03-01T10:00:00Z"));
}
@Test
void createdOrderAppearsInFindAll() {
Order order = service.create(new CreateOrderRequest("cust-7", List.of(line("A", 1, "5.00"))));
assertThat(service.findAll()).extracting(Order::id).containsExactly(order.id());
}
}
// If findAll() were filled asynchronously:
// await().atMost(Duration.ofSeconds(2)).until(() -> service.findAll().size() == 1);

Notice what changed. Each test name states a rule. Each assertion checks a specific value, so a wrong status or total fails with a message showing the actual value. Nothing depends on which repository method was called. The fixed clock makes the creation time exact and the test instant. And if one behaviour breaks, only the test for that behaviour fails, which tells us immediately where to look.

7Follow-up questions

Is verify() always a smell?

No. When the interaction itself is the behaviour, such as sending an email, publishing an event or calling a payment provider exactly once, verifying it with specific arguments is correct. The smell is verifying internal calls with any(), which checks neither the content nor anything the caller cares about.

Why prefer a fake repository to a mock?

A fake behaves like the real thing in a simple way, for example storing orders in a HashMap, so tests can check outcomes by reading data back. It survives refactoring of the service, and it is written once and reused across many tests, while mocks need their behaviour stubbed in every test and tie tests to call sequences.

What does Awaitility do that Thread.sleep does not?

It waits only as long as needed. It polls a condition until it is true and then continues, so a fast system finishes quickly, and it fails with a clear timeout message if the condition never becomes true. Thread.sleep always waits the full time and still fails if the system was slower than the guess.

8Conclusion

In this answer, we reviewed a short test and found five smells: a meaningless name, weak assertions, verification of an implementation detail, a fixed sleep, and several behaviours in one test. We rewrote it as two focused tests with behaviour names, specific assertions, an in-memory fake instead of verify(save(any())), and a fixed clock, and saw where Awaitility fits when code is truly asynchronous.

What to say in the interviewAbout 80 seconds aloud

Open with

I see five smells. First, the name test1 says nothing about the behaviour, so a failure report is useless; I would name it something like createsOrderWithPendingStatus. Second, the assertions are weak. assertNotNull only proves something came back, and size greater than zero only proves some order exists, so an order with the wrong status or total would pass.

Then, if they want more

I would assert specific fields: status pending, the customer id, the total, the creation time. Third, verify repo save with any checks that a method was called, not what was saved, and it couples the test to the implementation, so a harmless refactoring to saveAll breaks it.

I would check the outcome instead, by reading the order back, ideally with an in-memory fake repository. Fourth, Thread.sleep of 500 milliseconds makes every run slower and is flaky on a busy CI machine.

The service already takes a clock, so I would pass a fixed clock, and if something is truly asynchronous I would use Awaitility, which polls until the condition holds. Fifth, it tests creation, saving and listing at once, so a failure does not say which broke.

I would split it into two focused tests, each with arrange, act and assert, so each one checks one rule and fails with a clear message.

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.
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 "Review this test and name its smells: @Test": https://scrivio-interview-library.netlify.app/java/test-strategy/baa87095e1/