问题描述:

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 :

  1. Unit testing : JUnit In Action
  2. Regression test : Selenium
网友答案:

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:

  • Junit in Action 2nd
  • Test Driven - Practical TDD and Acceptance TDD for Java Developers (one of my favorites)
  • xUnit Test Patterns (Somehow a must have)
  • Practical Unit Testing (Well written using TestNG and Mockito, quite new)
  • Kent Beck about Test Driven Development
相关阅读:
Top