Quick revision
The short answer
TestNG creates one instance of CartTest and runs both test methods on it, so the cart field is initialised once and shared. When addsItem runs first, which it does here because TestNG orders methods by name when nothing else decides, startsEmpty finds a cart that already holds one item. The fix is to create a fresh Cart in a @BeforeMethod method; and since TestNG's assertEquals takes (actual, expected), the arguments in this class are in the right order for TestNG, unlike JUnit's (expected, actual).
What to remember
In this answer, we saw why the second test fails only when both run: TestNG uses one instance of the test class for all its methods, so the cart field is created once and addsItem leaves an item that startsEmpty then sees. JUnit Jupiter avoids this by default with a new instance per method. The fix is fresh state in @BeforeMethod. We also saw that TestNG's assertEquals takes (actual, expected), and that tests should never rely on the order TestNG happens to choose.
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.
TestNG creates one instance of CartTest and runs both test methods on it, so the cart field is initialised once and shared. When addsItem runs first, which it does here because TestNG orders methods by name when nothing else decides, startsEmpty finds a cart that already holds one item. The fix is to create a fresh Cart in a @BeforeMethod method; and since TestNG's assertEquals takes (actual, expected), the arguments in this class are in the right order for TestNG, unlike JUnit's (expected, actual).
Why this question is asked
Tests that pass alone and fail together are among the most confusing failures a team meets, and in TestNG this one has a specific cause that surprises people coming from JUnit. Interviewers use this small class to see whether we know how the framework creates test instances, whether we can explain the order dependence, and whether we know the assertion argument order. In this answer, we will reproduce the failure, see why TestNG shares the field, compare it with JUnit Jupiter, fix it, and look at the assertEquals order and test ordering.
1Reproducing the Failure
Here is the class as given, with the result of running each test alone and both together.
Each test is correct on its own. Together, the second one sees a cart with one item in it. That tells us the two tests are sharing something, and the only candidate is the cart field.
2Why TestNG Shares the Field
TestNG creates one instance of each test class for the run, then invokes all of that class's test methods on the same object. The field initialiser private Cart cart = new Cart() runs once, when that instance is constructed, not once per test. So both methods see the same Cart object.
addsItem adds one item and leaves it there. When startsEmpty runs afterwards on the same instance, the cart still contains that item, so size() returns 1. The failure depends on the order: if startsEmpty happened to run first, both tests would pass. That kind of hidden order dependence is exactly what makes these failures intermittent when the order changes, for example when someone adds a test method, renames one, or turns on parallel execution.
7 messages in order. Play them, or step through them one at a time.
The field is created once per instance, and TestNG uses one instance for all methods.
3How JUnit Jupiter Differs
JUnit Jupiter's default is the opposite. It creates a new instance of the test class for every test method, which is called the per-method lifecycle. In Jupiter, the same class would pass in any order, because each test gets its own freshly constructed object and its own new Cart. Jupiter offers @TestInstance(Lifecycle.PER_CLASS) to share one instance, but we must ask for it.
This is why teams moving between the frameworks get caught. Field initialisers are a safe habit in Jupiter and a hidden trap in TestNG. TestNG chose one instance per class partly so that expensive state in fields, such as a browser or a client, can be shared across methods without static fields, and that is useful, but it means per-test state must be created explicitly.
4The Fix: Fresh State in @BeforeMethod
The fix is to create the cart in a method that runs before each test. In TestNG that is @BeforeMethod.
Now each test starts from a known state, whatever ran before it. If the class is ever run with parallel="methods", the shared field becomes a new risk, because two threads could replace cart while the other is using it. In that case each test should create its own local Cart, which is the simplest design of all for a tiny object like this one.
5Two Details: assertEquals Order and Test Order
TestNG's org.testng.Assert.assertEquals takes (actual, expected). JUnit's Assertions.assertEquals takes (expected, actual). The class above uses assertEquals(cart.size(), 0), which is the correct order for TestNG, and the failure message reads expected [0] but found [1]. If someone migrates this test to JUnit without swapping the arguments, it still passes or fails correctly, but the message becomes misleading: expected 1 but was 0, which sends the reader looking for the wrong bug. Using AssertJ's assertThat(cart.size()).isEqualTo(0) avoids the question in both frameworks.
On order: without priority or dependsOnMethods, TestNG decides the order of methods in a class itself, and in practice it sorts them by name, which is why addsItem ran before startsEmpty. That is an implementation behaviour, not a contract. Tests should be correct in any order. If an order genuinely matters, such as a scenario test where step two needs step one, dependsOnMethods makes that explicit and reports later steps as skipped when an earlier one fails, but that should be the rare exception for unit tests.
6Follow-up questions
Would making the field final help?
No. final only stops the field from pointing to a different Cart; the Cart object itself can still be changed by add(). The problem is the shared mutable object, not the reference, so the fix is still a fresh instance per test.
Could we use @BeforeClass instead?
No, @BeforeClass runs once per instance of the class, which is exactly the shared lifetime that caused the bug. @BeforeClass is for expensive things that are safe to share, such as a read-only client; per-test mutable state belongs in @BeforeMethod.
How would you find such a dependence in a large suite?
Run the suite with a different method order or in parallel and compare results; tests that pass alone but fail together show up quickly. Searching for mutable fields initialised at declaration in TestNG classes, without a @BeforeMethod resetting them, also finds most cases.
7Conclusion
In this answer, we saw why the second test fails only when both run: TestNG uses one instance of the test class for all its methods, so the cart field is created once and addsItem leaves an item that startsEmpty then sees. JUnit Jupiter avoids this by default with a new instance per method. The fix is fresh state in @BeforeMethod. We also saw that TestNG's assertEquals takes (actual, expected), and that tests should never rely on the order TestNG happens to choose.
Open with
The cause is how TestNG creates test instances. It creates one instance of CartTest and runs all its test methods on that same object, so the field initialiser new Cart runs once and both tests share the cart.
Then, if they want more
TestNG orders methods by name here when nothing else decides, so addsItem runs first and leaves one item in the cart, and then startsEmpty finds size 1 and fails with expected zero but found one. Each test passes on its own, which is the giveaway.
JUnit Jupiter behaves differently by default: it creates a new instance for every test method, so the same code would pass there, and you have to opt in to sharing with TestInstance per class. That's why this catches people moving to TestNG.
The fix is to create the cart in a @BeforeMethod method, so every test starts with a fresh cart whatever ran before it; and if the class ever runs methods in parallel, I'd make the cart a local variable in each test instead. Two more details.
TestNG's assertEquals takes actual first, then expected, the reverse of JUnit, so this code is correct for TestNG, but if migrated without swapping, the failure messages become misleading. And I would never rely on test order; if a sequence really matters, dependsOnMethods makes it explicit.
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 "In this TestNG class, the second test fails only when both tests run:": https://scrivio-interview-library.netlify.app/java/testng/a3b40f5578/