• 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

Setting up test data in Junit

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anyone have some decent patterns for setting up, or loading test data for objects using Junit. There must be a better framework alternative than hard coding values to load objects.

I would like to have some sort of way in the setUp() method for a test case to load up a set of data structures (from a file in Excel or XML format) into objects for the test methods to use. Another great feature would be to pass in the file name to load up different scenarios for a method.

Thanks,
John M. Brown
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Automating build and test
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, what you describe is certainly possible but I'm not aware of a TestCase implementation that would do that for you.

Personally, I don't find hardcoding the state of a test harness in the setUp() method to be a big deal. Could you post some example code of what you're doing now?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, you might want to take a look at JXUnit: http://jxunit.sourceforge.net/

As Lasse, I also wonder what you are doing, though...
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
Personally, I don't find hardcoding the state of a test harness in the setUp() method to be a big deal.


By the way, I meant to say fixture instead of harness...
 
author & internet detective
Posts: 41878
909
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
John,
Are you trying to get a lot of data that all the tests will share or to run the same tests with different data?

If the later, look into the parameterized test case pattern (search the junit group archives.)
 
John M Brown
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,
It's the latter. I've hardcoded some object setups for the sunny day scenario of just a few methods on a controller, but there are many combinations of objects that need to be tested and a lot of different outcomes based on the tests.

Plus trying to keep the input in sync with any setup data as the databases and data models are changing (and sometimes the other code and interfaces) is a maintenance nightmare. It would be much easier to populate the objects from spreadsheet that could be maintained at a business level. The data values for the tests can get out of date with the system fast and often, which requires changes to the java TestCase code.

The unit tests we have used for XML messaging has been easier, because we can setup multiple test XML files that represent the messages and the TestCases can load the XML data into the objects using JAXB, DOM, or SAX.

Thanks,
John

Thanks for all the suggestions, I will look into them.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John M Brown:
The unit tests we have used for XML messaging has been easier, because we can setup multiple test XML files that represent the messages and the TestCases can load the XML data into the objects using JAXB, DOM, or SAX.


What exactly is the difference between this and reading the test data from a spreadsheet (which is also an external file)?
 
John M Brown
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:

What exactly is the difference between this and reading the test data from a spreadsheet (which is also an external file)?



Lasse,
There's actually little difference, just the format of the input. For the EAI application I worked on, the XML data was being passed into methods that can take XML, or at least objects that bind to XML (JAXB). Which took care of the loading. I don't have that situation for the application I'm working on now. The objects are POJOs.

Actually any framework that takes in spreadsheets or XML would be great.

I'm looking into the JXunit stuff now to see if it will work.

Thanks,
John
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey John

I know its kinda late to reply to this Still I thought I would tell you what I did when i faced the same problem. I have done exactly what you need to do.DBUnit is the answer.It is something that is built on top of JUnit.In the setUp() you can load the data from an excel and in the tearDown() you can delete the data.Not only that, it also gives you the privilege of comparing resultsets obtained from database to an excel sheet which has the expected result set!

Its not as easy as it sounds though.You will need to write a hew helper classes to make your life easy but once those are in place the sailing is so smooth that you wont regret the time spent.

I can give you more details but lets be sure if this is what you were looking for.In case you were feel free to contact me.

regards
Prashant Jain
prashantjainn@yahoo.com
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic