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.
The moose likes Testing and the fly likes JUnit; is there a way to order tests? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit; is there a way to order tests?" Watch "JUnit; is there a way to order tests?" New topic
Author

JUnit; is there a way to order tests?

Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
Hi,

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.

Regards,
Sim085
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 193

Yes. you are right. Junit needs the order for executing testcases.

For executing test cases we have setUp() and tearDown() methods.
For setting up common dependencies you need to setup data in setUp() method. tearDown() after execution.

If the dependencies are not common to all test cases in a Test class, you need to stub the data before executing or you need to execute them in the order they need to execute.


SCJP 5, SCWCD 5
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
Prabhakar Reddy Bokka wrote:or you need to execute them in the order they need to execute.


How do you execute the test cases in the order they need to be executed?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Tests should not have dependencies like that, but you can construct your own TestSuite.
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 193

How do you execute the test cases in the order they need to be executed?


You can write the methods in the order they need to execute.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Prabhakar Reddy Bokka wrote:
How do you execute the test cases in the order they need to be executed?


You can write the methods in the order they need to execute.

If that works, it is coincidence and by no means guaranteed.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Simon,
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?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In fact the JUnit FAQ specifically states that test execution order is not guaranteed. [1]

You need to construct a test suite--but unit tests should be as isolated and independent as possible.

[1] http://junit.org/apidocs/org/junit/runner/Request.html
Simon Joseph Aquilina
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
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.

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

First of all, these aren't unit tests. They are integration tests. Unit tests wold be truly independent.

With integration testing, you have two options.

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
Ranch Hand

Joined: Feb 14, 2006
Posts: 102
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Personally I prefer something higher-level for integration tests like easyb, rspec/cucumber/shoulda/etc., or spock, which I haven't used yet.

Of course, I prefer something higher-level for unit tests, too, but that's a different issue :)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Simon,
I use JUnit for my integration tests as it serves my needs just fine.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JUnit; is there a way to order tests?