简体   繁体   中英

Unit test Spring DAO

I wrote an API with Spring+Hibernate+postgres, I have the followinq questions

1) I am trying to unit test my DAOs for CRUD operations. whats the best way to do that, does testing on temporay datastore like H2 be the same?

2) Is there any suggested tutorial to do this?

Please help

You may want to use a Spring profile for testing and just swap your DataSources with a embedded H2 Database that will run only on your test. The Spring reference explains it very well. It is as easy as:

<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:schema.sql"/>
    <jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>

The id attribute will be the id of the bean of type DataSource.

You may want to have scripts to set up test records like in the example.

The best way to test DAOs is by test-applicationContext to use a in-memory H2 database, you could also use mockito framework.

See this Hibernate temporary datastore http://iamjosh.wordpress.com/2007/12/11/unit-testing-dao-classes-with-junit-spring/

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM