Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static members and testability

 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use several static members, ie my Data has a static LockManager and static DataFileAccess.

When I'm JUnit'ing this causes all sorts of headaches. I have a setup/teardown which provides each test case a clean Data impl accessing a clean data file. Makes sense huh?

BUT becaus I use static instances, they are left over in the JVM, and so my clean Data instance, is'nt really clean. Each test case has to know what the other test cases have done.

Similar problems occur if you use static members in the DataFileAccess or LockManager.. thankfully I don't need to have static members in these classes.

Andrew, I know you use a similar solution in your book, and your DataAccess and LockManager classes have static members. Do any thoughts on this issue?
It also kind of reduces the re-usability of the code in theory.. would you aggree?

The most granularity I can get in calling the test cases is a new JVM per test case class. Otherwise I'd have to split ALL the test cases into their own classes .. okay I could make a base class and extend, still too much effort.
 
Ed Tse
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering why they would be static? ie, DataFileAccess, does it mean you can not create two Data object for two different data file?
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in the same JVM thats true.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

You might be interested in reading "Anybody using developing their project and adding UNIT Tests?". In that, Roel De Meester provides his JUnit test cases, and you may find your answer in how he handles it.

Regards, Andrew
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've read through this - it does'nt seem to touch on the specifics that I'm thinking of.

Anyone else any idea's?
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies - I was'nt look hard enough apparently
 
Alan Mc Kernan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't particularly like this way of testing the instance with static members. It requires changing the actual code your testing in order to test it (from what I can see).

Ideally we just want to spawn a new JVM for each test case. This should be possible, but maybe I have to change junit a little?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic