This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I was wondering does JUnit has any option to order tests? I have a test that depends on another test and at the moment I can only show that these tests are dependent on each other by placing the methods under each other. My example is as follows; I have a first test that tests if an object can be persisted in database. The second test (in a different method) tests if the object can be loaded from database. Of course if the second test is executed before the first one then it will fail. JUnit seems to run tests after each other as written in code. However I would like to know if there is a more solid way how to show test dependencies.
In JUnit 4.5 you can create a test suite by specifying the class and method for each test. This is tedious and bad practice. In JUnit 4, I don't see a way of doing that. Maybe you create your own runner.
The thing is, you don't really have a bunch of separate tests if they can't run independently. You have one giant test in multiple methods. Why not just call them one test and change the other "tests" to private methods?
Yes, but let me give a little example. Imaging I have the following class;If I want to unit test the above class I see it natural to first test if save object works and then test if load object works. However it is obvious that to load an object you first need to save it. I could therefore implement my tests as followsNow, if I have problems in the save method I would like to capture that when testSaveObject() is executed rather then when testLoadObject() is executed since otherwise this may give the wrong impression that it is the load method which does not work rather then the save method. Also if I could make testLoadOBject() depend on testSaveObject() the I could change my test implementation as follows;This feels like a more natural way to write tests for me. However as I said above I am open to any suggestions of how this can be better written.
author & internet detective
1) Setup the data using something other than your API. (maybe raw SQL.) This is usually used when neither of the APIs exists yet and you are building the first one.
2) Merge them into one bigger test:
Simon Joseph Aquilina
Joined: Feb 14, 2006
Jeanne Boyarsky wrote:First of all, these aren't unit tests. They are integration tests. Unit tests would be truly independent.
Thanks for your reply. I understand better now and have adapted my tests to be similar to the one you described. However I have just one other question; Given that these are more like integration testing rather then unit testing, do you still consider JUnit as the most ideal tool/framework? Or you would suggest another one? I am mostly doing these tests to learn how best to test an application so I do not mind using anything new if it's better for the job.