要求:
  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());
}
}