Junit4测试dao,不光是可以测试bug,还可以提高开发速度,初始化数据,为后面的页面调试提供方便
需要jar包
spring-test.jar
junit4.8.2.jar
package org.virus.hibernate; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit45ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit45ClassRunner.class) @ContextConfiguration(locations = { "classpath:/config/spring/applicationContext.xml", "classpath:/config/spring/applicationContext-hibernate.xml" }) @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) @Transactional public class BaseDAOTestCase { }
package org.virus.hibernate; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Iterator; import junit.framework.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.virus.model.PagerModel; import org.virus.model.Serology; import com.opensymphony.xwork2.interceptor.annotations.Before; public class SerologyHibernateTest extends BaseDAOTestCase { private SerologyManager serologyManager; @Autowired public void setSerologyManager(SerologyManager serologyManager) { this.serologyManager = serologyManager; } @Before public void init() { } @Test public void testSaveOrUpdate() { for (int x = 0; x < 30; x++) { Serology s = new Serology(); //s.setId(x); s.setKit("kit" + x); s.setOrganism("organism" + x); s.setVirusName("virusName" + x); s.setNote("note" + x); serologyManager.saveOrUpdate(s); } PagerModel pm = serologyManager.find(0, Integer.MAX_VALUE); Assert.assertTrue(pm.getTotal() > 0); } @Test public void testFind() { PagerModel pm = serologyManager.find(0, Integer.MAX_VALUE); for (Iterator<?> iter = pm.getList().iterator(); iter.hasNext();) { Serology o = (Serology) iter.next(); assertNotNull(o.getVirusName()); } assertTrue(pm.getTotal() > 7); assertNotNull(pm.getList().get(1)); } }
测试集
package org.virus.hibernate;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**/
@RunWith(Suite.class)
@SuiteClasses({CloneManagerTest.class,KitManagerTest.class,SerologyHibernateTest.class,ProbeManagerTest.class})
public class TestAll {
}