I have the object with static field:
class Project() {
private static id;
private int projectid;
public Project(fileds) {
this.id = id++;
}
//methods
}
Now I want to test this class with multiple tests. The problen is that my objects are not deleted from memory when one test is completed:
@Test
public test1(){
Project project1 = new Project();
Project project2 = new Project();
}
@Test
public test2(){
here the objects from previous tests are still exist since the static field is two times increased
}
Is there any way I can flush them after each test? Since the only way I can overcome it - using Ignoring...
I don't think this is well written.
If I interpret this correctly, you want a unique
projectid
associated with each instance that is calculated from the static count. Change your code like this:This way
projectid
will start with zero and increment by one each time you create a new instance.You shouldn't be worrying about flushing or what the project id count is. That's not material for your method tests.
If you must reset to zero, make the static variable public:
Here's how you reset it in your tests (if you must):