Jfixture is a tool for loading data from yaml files into a SQL database with a focus on making it easy to test database-dependent code.

You can get the code from Maven central:

<!-- Optional, but handy if you're using Hibernate -->

A simple example of the yaml format used by jfixture follows below. It contains one fixture (a conceptual grouping of rows across multiple tables typically used to house all of the rows necessary for a test or group of tests) with three rows in two tables representing a hypothetical blogging system. The 'article' row will have its articleId populated with whatever the database assigns to that column (assuming it's an auto-generated primary key). The other two rows in 'comment' will then use that id, avoiding the need to hard-code foreign keys.

        articleId: ${autoId}
        contents: blog post goes here. lorem ipsum dolor sit amet...
            articleId: ${this.article.articleId}
            message: nice post
            position: 0
            articleId: ${this.article.articleId}
            message: This is a spam comment. Buy v1agr4.
            spamScore: 5
            position: 1

The 'this' inside the ${} part is used because it's referring to a row in the same fixture. You can also refer to rows in other fixtures as well as to ones in different databases.

Inside your Java code, you can use code like the following (here done JUnit 4 style):

private FixtureController fixtureController;

public void setUp() {
    fixtureController = new FixtureController();
    // assign a particular data source to the identifier "main"
    fixtureController.addDb("main", yourCodeToGetDataSource());

    // you can provide the yaml as an InputStream, Reader, etc
    fixtureController.setDbContents("main", yourCodeToGetYamlData());

    // once you're done setting up all your DBs, write the data

public void tearDown() {
    // empty every table that had fixture data written to it

    // also empty other tables that you may have written to in your code

The database identifier ("main" in this example) is just a nickname for you to use to refer to that database. It doesn't need to have any relation to the database name used in the underlying DBMS.

The above yaml file and code would cause the following SQL to be executed in setUp():

INSERT INTO article (contents) VALUES ('blog post goes here. lorem ipsum dolor sit amet...')
-- suppose that the above insert created the row with articleId 94
INSERT INTO comment (articleId, message, position) VALUES (94, 'nice post', 0)
INSERT INTO comment (articleId, message, spamScore, position) VALUES (94, 'this is a spam comment. Buy v1agr4.', 5, 1)

and in tearDown():


If you wish to access your user row's data in your test code, you can ask the fixture controller:

Value contentsValue = fixtureController.get("main", "blogPostWithComments", "article", "contents");
String contentsString = contentsValue.asString();
Value idValue = fixtureController.get("main", "blogPostWithComments", "comment", "firstComment", "articleId");
int idInt = idValue.asInt();

That's the very basics. For more, check out the user guide.