user994165
user994165

Reputation: 9502

Parent classes in JUnit tests

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

Answers (3)

Kelly S. French
Kelly S. French

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

Mustafa Ulu
Mustafa Ulu

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

Florian Schaetz
Florian Schaetz

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

Related Questions