ThatOneGuy
ThatOneGuy

Reputation: 160

Resetting a private static int from a junit test class

I am trying to write some junit tests for a class called Student. Basically, each student has a studentNum which is being set to an iterator which is a private static int. Every time a new Student is created, studentNum is incremented.

I have several tests for a function that gets the Student with a studentNum of 1 from a passed in arraylist of Students. However, each time I make a new arraylist of students in a new test, studentNum starts where the previous test's studentNum left off. So the first test will make Students with studentNums from 0 through 5, and the second test will make students with studentNums from 6 through 11.

I was wondering if there is a way to reset the private static studentNum integer from my test class, so that I can have it start at 0 for each test? Any help would be greatly appreaciated.

Upvotes: 0

Views: 4412

Answers (5)

Vadim
Vadim

Reputation: 4120

Just think about what static decalration means... For real purposes studentNum must not be static if it holds Student distinctive number. with static all your Student objects will have latest studentNum.

But if it is a requirement (cannot imagine for what ...), just for junit with multiple @Test methods (and only with) R O M A N I A is correct. Do this:

@Before
public void setUp() throws Exception {
    Student.studentNum =0;
}

It will reset static studentNum before execution of each @Test method call.

Upvotes: -1

JChrist
JChrist

Reputation: 1752

You can have a @Before or an @After (or both) in which you reset the private static field to whatever value you wish (e.g. 0), using Java's Reflection API.

The way to do that would be:

@Before
public void setup() throws Exception {
    Field studentNum = Student.class.getDeclaredField("studentNum");
    studentNum.setAccessible(true); //to overcome the visibility issue
    studentNum.setInt(null, 0); //null since it's static
}

Upvotes: -2

biziclop
biziclop

Reputation: 49734

The fact that you find this hard to test is a warning sign that you probably need to rethink your design. Ask yourself this: why should the Student class be responsible for generating a unique id?

If you separate the id generation logic (even if it's as simple as incrementing a single counter) into a separate class, suddenly you'll be able to mock that class while you're testing Student and have it return any id you want in your tests.

Upvotes: 3

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13773

Have a look at @Before/@After annotations. Methods annotated like this get called before/after each test case is run. You can reset your data there.

@Before
public void setup(){

}

Upvotes: 0

nasukkin
nasukkin

Reputation: 2540

each student has a studentNum which is a private static int

This statement makes no sense. If each instance of your Student object is to have its own id, the id field should not be static, by definition.

Upvotes: 2

Related Questions