dimanche 28 août 2016

JUnit testing static counter of objects

I am quite a beginner at unit testing and I got some failures that I do not know how to solve. I was trying to test my simple class Employee where I have static counter of created objects, so new employees can get consecutive numbers and default names like "Name1", "Name2" etc. Here is my default initiaiton block:

    {
    currentNr = ++count;
    setName("Name"+currentNr);
    setSurname("Surname"+currentNr);    
}

I wrote one JUnit class with few methods. They are working fine but methods concerning counter are working only when I run them separately (they were also working when I saved them as separate tests, but it seemed messy having so many files). When I run the class with all the testing methods, counter is adding more object and I do not know why/when/where as test are independent. In testing methods I am creating an object and checking the counter with assertEqual. Looking for solutions I tried to work with @Before, @After, etc. but it was the same or maybe I do not know how to use it properly. My question is what could I do to have all the test methods working or what should I write in @Before method (I tried adding and deleting objects to ArrayList and/or setting to null). I guess it is not acceptable to have test working only when run separately. Any help will be appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire