JUnit4 + spring 测试实例

 下载jar包:

 junit测试:加入junit-4.12.jar和hamcrest-core-1.3.jar和spring-test-4.1.6.RELEASE.jar
 hamcrest-library-1.3.jar

http://search.maven.org/#search%7Cga%7C1%7Cfc%3Aorg.hamcrest.Matchers

http://junit.org

import java.util.Date;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.domain.User;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserMapperTest {
 
 @Autowired
 //@Resource
 private UserMapper userMapper;

 @Test
 public void test1(){
  
  Assert.assertNotNull(userMapper);
  
  User user=new User();
  user.setUserId("200");
  user.setUserName("xx");
  userMapper.insert(user);
  
  user=userMapper.selectById("200");
  //Assert.assertSame("xx", user.getUserName());
  Assert.assertEquals("xx", user.getUserName());
  
  user.setBirthday(new Date());
  user.setSalary(3000);
  userMapper.update(user);
  
  user=userMapper.selectById("200");
  //Assert.assertSame(3000, user.getSalary());
  //Assert.assertEquals(3000, user.getSalary());
  Assert.assertEquals(3000, user.getSalary(), 2);
  Assert.assertThat(user.getSalary(),  org.hamcrest.Matchers.greaterThan(100d));
  
  int i=userMapper.delete("200");
  user=userMapper.selectById("200");
  Assert.assertTrue(user==null);

 }

}

猜你喜欢

转载自zw7534313.iteye.com/blog/2221099