Remember One Another

An online tool to help Christians pray more effectively

The Junit TestCase and Instance Variables

Posted by jobber on June 13, 2008

Today I continued to work on revamping our tests, with two immediate goals in mind.

First, I needed a test that would automatically populate a database, saving me the trouble of a lot of data entry.

Second, we needed tests that worked with MySql, not just hypersonicSQL, which we have been using for our tests.

Once I accomplished these goals, I could proceed to change a property type in my local database, and begin experimenting with database diff tools to see what types of good scripts they could generate between the local and web databases.

This was all in the interest of facilitating database versioning and implementing significant table and property changes on live databases.

Mr. Chandler made an interesting find today. He had previously VI-ed into the my.cnf file of /etc/mysql/ on my laptop. the MySql config file, to try to allow his computer access to the databases running on my local machine. The property ‘bind-address’ in this file was currently bound to 127.0.0.1 (which is localhost). He edited it to his computer’s IP, but we figured out today that now my machine couldn’t log in! This was, of course, because localhost was no longer the bind address.

It seems that MySql allows either one or all address in the bind address. Our fix was to enter the address ‘0.0.0.0′, representative of all addresses.

Today’s Note for Dense Summer Interns:

“…at runtime, each test case executes as an instance of your subclass of TestCase.”

This sentence in the office copy of J.B. Rainsberger’s book, JUnit Recipes is now highlighted bright orange. Mr. Chandler and I spent about three hours today trying to get one of our tests running properly because we did not recall this fact. Each test method in a JUnit Test (annotated @Test) will create new instances of class fields; therefore, changes to a variable inside of a TestCase instance will not affect other TestCase instances! We learned this the hard way.

(While learning this lesson the hard way we were forced to learn many other valuable lessons about Spring @Transactional, Spring rollback, and Hibernate persistence. One could argue that JUnit is still doing its job by forcing us to understand what we are doing.)

So, what if you want to change a variable in one test block and have it influence another block? We met with some success declaring our class fields static.

Today’s Linux keyboard shortcut: cntrl-alt-d. Minimizes all windows. Handy for those times when you’ve just sorted through ten windows and realize that the file you are looking for is now underneath all of them on the desktop. I will have to try this out on Windows, but I am afraid it may be a Linux exclusive.
Update: Mr. Chandler let me know that this shortcut is indeed available to Windows users, in the form of the windows key+d.