Spring2.5+JUnit4单元测试

发表于:2014-6-05 10:41

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:william_yao    来源:51Testing软件测试网采编

  要求:
  JDK1.5以上(因为Junit4是用注解来实现的)
  需要的包
  spring-2.5.jar
  junit-4.4.jar
  spring-test.jar
  测试
package user;
import static org.junit.Assert.fail;
import java.util.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import com.sample.model.user.User;
import com.sample.service.user.IUserService;
/** 设置要加载的配置文件 */
@ContextConfiguration(
locations={
"classpath:spring/persistenceContext.xml",
"classpath:spring/aopContext.xml",
"classpath:spring/daoContext.xml",
"classpath:spring/serviceContext.xml"
}
)
/** 设置是否回滚数据 */
@TransactionConfiguration(defaultRollback = false)
public class UserTest extends AbstractTransactionalJUnit4SpringContextTests{
/** 设置自动注入的属性 */
@Autowired
private IUserService userService;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
@Rollback(false)
public void testSaveUser() {
User user=new User();
user.setUsername("zhoujun");
user.setCreatetime(new Date());
userService.saveUser(user);
}
@Test
public void testGetUserById() {
User user=userService.getUserById("1");
System.out.println(user.getUsername());
System.out.println(user.getCreatetime());
}
}
31/3123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号