Reputation: 9502
I have a String defined as a protected variable in a parent class of a JUnit class. In one of the JUnit tests I change the value. In the next JUnit test, the value goes back to being the original value. Is this something specific to the way JUnit works? When I test this out with regular classes, any changes to the parent variable in the child is preserved.
Upvotes: 0
Views: 1937
Reputation: 12334
JUnit runs all tests in separate instances and in no specific order. If you need there to be some order or state to a set of tests you need to create a TestSuite.
Upvotes: 0
Reputation: 185
JUnit creates different instances for every test case. When you change a field inside a test method only one instance see that change. Other test methods have their own instances.
Martin Fowler's bliki entry on this
Upvotes: 3
Reputation: 10652
Are you sure that you don't simply assume that the test that changes the value runs before the test that checks it? Normally you should not assume any order of the JUnit tests.
Upvotes: 1