1

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.