Spring Junit4功能介绍
SpringJUnit4BaseTest.java文件
/** * */ package springjunit4; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; /** * @author Administrator * */ @ContextConfiguration(locations="classpath*:core/persistence/beans.xml") @RunWith(SpringJUnit4ClassRunner.class)//或者SpringJUnit4BaseTest extends AbstractTransactionalJUnit4SpringContextTests @Transactional @TransactionConfiguration(transactionManager = "txManager",defaultRollback = true) public class SpringJUnit4BaseTest{ }
JunitTest.java文件
/** * Copyright (C) 2006-2012 Tuniu All rights reserved * Author:tianzhipeng * Date:2012-7-9 * Description: */ package springjunit4; import java.util.HashMap; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import junit.framework.Assert; import org.junit.Test; import core.domain.User; import core.service.IUserService; /** * Description : Menpiao unit test */ //@SpringApplicationContext({ "classpath*:core/persistence/beans.xml" }) //@RunWith(UnitilsJUnit4TestClassRunner.class) public class JunitTest extends SpringJUnit4BaseTest{ @Resource private IUserService userService; @Test public void testSaveMenpiao() { Map<String, Object> data = new HashMap<String, Object>(); User user = userService.getUserByName("tom"); System.out.println(user.getName()); Assert.assertEquals(user.getName(), "tom"); } @Test public void testGetRequest() { HttpServletRequest request = null; } }
Eclipse debug快捷键:Alt+Shift+D,然后按T键。
需要jar包:junit-4.10.jar,spring.test-3.1.1.RELEASE.jar,spring-tx-3.1.0.RELEASE.jar