Henrik Bierbum Bacher
Henrik Bierbum Bacher

Reputation: 80

Runinng SQL script from JUnit

In the setup method to a JUnit test case I'm working on I need it to run a sql script on my database before each test case, and then a rollback afterwards.

I have tried using a tokenizer, which added each SQL command to a batch and then executing them. But I can't get working. So my question is if there is some standard method in JUnit to perform this action?

Upvotes: 1

Views: 4878

Answers (2)

Aaron Digulla
Aaron Digulla

Reputation: 328594

You can try DbUnit

DbUnit is a JUnit extension (also usable with Ant) targeted at database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.

DbUnit has the ability to export and import your database data to and from XML datasets. Since version 2.0, DbUnit can also work with very large datasets when used in streaming mode. DbUnit can also help you to verify that your database data match an expected set of values.

Upvotes: 2

guerda
guerda

Reputation: 24049

It's not the task of JUnit to test SQL statements.

You should create a Mocker (EasyMock e.g.) and isolate the connection. So the mocker can imitate the sql connection and its results. With this mocking object, you can check, if your sql connector class called the right statements.

If you want to test the SQL statement, its results and so on, you should use DBUnit, as Aaron said.

Upvotes: 1

Related Questions