How can I write JUnit tests for CRUD operations of an application(Java application). I use MyBatis as data mapper over database.
For example I have a query as this:
SELECT * FROM sm.PERSON WHERE name='something'
And I call this query from MyBatis:
java.util.List<Person> persons = SqlMapClient.openSession().queryForList(queryName)
Now how can I test this operation to get that the result is right?
(Another thing please tell me title of books for learning Testing in java. I know the basics and I want to get deeper.)
I think your Junit test should run standalone without depending on database results. In your case you need to test using the database, you can use DbUnit, an junit extension targeted for database-driven projects.
For the ebook :
Here, it seems that you want to
test MyBatis's functionality of its CRUD functions using JUnit. What I would have done is,
use the native JDBC which is an established standard to fire the query over the database and
then compare the result with what the
MyBatis function gives.
For your second question, if you are using
JUnit 3, then use
JUnit in Action first edition and if using
JUnit 4, use
JUnit in Action second edition.
First question: testing of the persistence layer is not easy. You should read chapter 17 in Junit in Action 2nd edition. Unitils could be also help.
Books about Testing: