SSH集成測試,spring3.0,如何單元測試

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.bjmorning.sys.dao.UserDao;
import com.bjmorning.sys.model.User;
import com.bjmorning.sys.service.UserService;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;

@RunWith(SpringJUnit4ClassRunner.class)
// 指定測試用例的運行器 這裏是指定了Junit4
@ContextConfiguration( { "/applicationContext.xml", "/hibernate.cfg.xml" })
// 加載所有需要的配置文件
// 指定Spring的配置文件 /爲classpath下
// @Transactional //對所有的測試方法都使用事務,並在測試完成後回滾事務
public class UserServiceTest {
	// @Autowired
	// private ApplicationContext appplicationContext;
	// //自動注入applicationContext,這樣就可以使用appli*.getBean("beanName")
	@Resource
	// 會自動注入 default by type
	private UserDao userDao;
	@Resource
	private UserService userService;

	@Before
	// 在每個測試用例方法之前都會執行
	public void init() {
	}

	@After
	// 在每個測試用例執行完之後執行
	public void destory() {
		userDao = null;
		userService = null;
	}

	@Test
	// @Transactional
	// 使用該註釋會使用事務,而且在測試完成之後會回滾事務,也就是說在該方法中做出的一切操作都不會對數據庫中的數據產生任何影響
	// @Rollback(false) //這裏設置爲false,就讓事務不回滾
	public void testFind() {
		Assert.notNull(userDao);
		Assert.notNull(userService);
		User user = userDao.findByLoginName("admin");
		Assert.notNull(user);
	}
}


發佈了89 篇原創文章 · 獲贊 10 · 訪問量 23萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章