aspose file tools*
The moose likes Testing and the fly likes Best practice for organizer DBUnit xml files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Best practice for organizer DBUnit xml files" Watch "Best practice for organizer DBUnit xml files" New topic
Author

Best practice for organizer DBUnit xml files

Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
I haven't found any discussion about how to organize the xml files needed to drive DBUnit based tests. In fact the only mention I've found is that they must be in the class path.

I've tried three places so far, but non have worked:
a) alongside the .java file test case. I've confirmed that its copied over to the classes directory during the compilation stage. But I get a file not found error.
b) in test/resources/. I've confirmed that Maven does move this file over to the test-classes directory, but I still get a file not found when the test runs.
c) manually copied the xml file into the root of the production code's classpath (target/[War-name]/WEB-INF/classes/). Still file not found
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Could you post the snippet of code that you use to process the XML files?


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Here's my abstract base class used in acceptance testing: (Having to manually retype it due it being on a non-networked computer at present, please excuse any minor typos)



and a sample test case using it:

[ September 25, 2007: Message edited by: Eric Nielsen ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
To me it seems like "src/test/resources/homePage.xml" as the path should work as long as you're running the test from the project's root directory (i.e. with Maven) or from an IDE (at least Eclipse uses the project's root directory as the working directory when running tests).
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
So if its not working in either eclipse or maven, where should I look to figure out what the problem is?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You could try and see which directory the following line prints out when you run your tests:



If that doesn't point to your project's root, then that's the root of the problem. If it does, then I really don't know what's happening.
Eric Nielsen
Ranch Hand

Joined: Dec 14, 2004
Posts: 194
Yes it was pointing to the correct place, so using a relative path like "src/test/resources/filename.xml" is working now. So rather than the directions I found on the DbUnit page about placing them on the classpath, it seems better to have them in the source tree?

Are there best practices out there for how people like to organize their db fixture files?

I mean for acceptance/integration tests, I'm aiming to use near minimal, but still slightly complex data sets. Ie representative, so only a few fixtures should be needed to cover most of the tests and they can live simply in a sub-folder someone.

However the fixtures for the service layer unit tests are likely to be truly minimal so so there's likely to be more proliferation of fixture files. Are there best practices for arranging/naming these to help keep things organized?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Eric Nielsen:
Are there best practices out there for how people like to organize their db fixture files?

I mean for acceptance/integration tests, I'm aiming to use near minimal, but still slightly complex data sets. Ie representative, so only a few fixtures should be needed to cover most of the tests and they can live simply in a sub-folder someone.

If you're using the Maven Standard Directory Layout like I am, you could indeed place all those data fixtures in some subdirectory like "src/test/resources/dbunit". I do this, too, when I have fixture files or other testing resources that are used by more than one package of test code.

Originally posted by Eric Nielsen:
However the fixtures for the service layer unit tests are likely to be truly minimal so so there's likely to be more proliferation of fixture files. Are there best practices for arranging/naming these to help keep things organized?

What I usually do is put the fixtures right next to the test code in my source tree, naming the XML files with the test class name as the prefix. For example, if I have an integration test class named "TestBhingleWithCleanZiffer", I might have fixture files called "TestBhingleWithCleanZiffer-initial.xml" and "TestBhingleWithCleanZiffer-expected.xml" that would contain the initial fixture my test is populating into the database and, optionally, the expected data set after some operation.

I like this practice because the fixture files are always close to my test code and I don't feel that it clutters up my test source tree. My second option would probably be to put the fixture files in another source tree using the same package structure to keep them organized.
Ed Dh
Greenhorn

Joined: Mar 22, 2007
Posts: 4
I like the convention to put the xml files right next to the testclass.
However running my testclass as a JUnit4 test and Spring's TestContext framework from within Eclipse (Maven nature) my dataset.xml file is not found.
When I look in test-classes I can only find my testclass, not my dataset.xml file.
Why are only my classes copied and not the xml file ?
PS. When I move my dataset.xml to my test/resources it does get copied over to the root of test-classes. But I don't like that. I want my dataset.xml to be copied over next to my testclass.
How can I achieve this ?
Thanks,
EDH
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best practice for organizer DBUnit xml files
 
Similar Threads
Organizing Testing tool JARs Question
Testing database queries???
Using Maven in Eclipse
DbUnit and Hibernate hbm2ddl.auto, best practices...
dbunit