Runinng SQL script from JUnit

In the setup method in the JUnit test case, I am working so that I have to run the sql script on my database before each test case and then rollback afterwards.

I tried using a tokenizer that added each SQL command to the batch and then executed them. But I cannot work. So my question is, is there any standard method in JUnit for doing this?

+1


source to share


2 answers


It is not a JUnit task to test SQL statements.

You have to create a Mocker ( EasyMock , etc.) and isolate the connection. This way mocker can simulate sql connection and its results. With this mocking object, you can check if your sql connector class raised the correct assertions.



If you want to test a SQL statement, its results, etc., you should use DBUnit as Aaron said.

+1


source


You can try DbUnit



DbUnit is a JUnit extension (also used with Ant) for database projects that, among other things, put your database in a known state between test runs. This is a great way to avoid a lot of problems that can arise when a single test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.

DbUnit has the ability to export and import database data to and from XML datasets. Since version 2.0, DbUnit can also handle very large datasets when used in streaming mode. DbUnit can also help you check if your database data matches the expected set of values.

+2


source







All Articles