In the unit-testing for database access code, some testing frameworks such as DbUnit are available. However, as you know, to maintain test data is very painful.
Mirage proposes lightweight unit-testing without database for database access code. It's based on string matching of executed SQL and expected SQL. Executed SQLs don't reach to the database actually, they are intercepted and recorded to MirageTestContext by using MockSqlManager.
You can verify executed SQLs and these parameters through MirageTestContext as below:
@Test public void testGetBookById(){ // set MockSqlManager to the test target BookService bookService = new BookService(); bookService.setSqlManager(new MockSqlManager()); // configure results which would be // returned by MockSqlManager. Book book = new Book(); book.setBookName("Mirage in Action"); MirageTestContext.addResult(book); // execute Book result = bookService.getBookById(new Long(100)); // assert returned object assertEquals("Mirage in Action", book.getName); // verify the number of executed SQL MirageTestContext.verifySqlCount(1); // verify the executed SQL MirageTestContext.verifySqlByRegExp(0, "SELECT .* FROM BOOK " + "WHERE ID = ? ORDER BY ID ASC", // verify SQL new Long(100)); // verify Parameters }
As you see, you can also configure return values of MockSqlManager using MirageTestContext if it's necessary. You can write your test case more simple by static importing of MirageTestContext members.
Please note, the purpose of this way is not to verify whether executed SQLs are correct. It helps to find influence which is not anticipated by modification of database access code. So it requires to current database access code and SQLs are correct.
If your purpose of unit-testing for database access code is matches this concept, Mirage will decrease a cost of unit-testing and maintaining them very much.
0 件のコメント:
コメントを投稿