• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JUnit; is there a way to order tests?

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Simon Joseph Aquilina
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tests should not have dependencies like that, but you can construct your own TestSuite.
 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 198
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon,
I use JUnit for my integration tests as it serves my needs just fine.
reply
    Bookmark Topic Watch Topic
  • New Topic