1,在eclipse中安装TestNg插件,这里省略。。
2,编写测试spring Dao层的代码
package test.com.smart.dao;
import com.smart.dao.UserDao;
import com.smart.domain.User;
import com.smart.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import java.util.Date;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
@ContextConfiguration(locations = {"classpath:resources/applicationContext.xml"})
public class UserDaoTest extends AbstractTestNGSpringContextTests {
@Autowired
private UserDao userDao;
@Test
public void hasMatchUser() {
int count = userDao.getMatchCount("admin1", "123456");
assertTrue(count>0);
}
@Test
public void findUserByUserName() {
User user = userDao.findUserByUserName("admin");
assertNotNull(user);
assertEquals(user.getUserName(), "admin");
}
}
注意:@ContextConfiguration(locations = {"classpath:resources/applicationContext.xml"})这个注解是的localtion属性使用的是src类路径下面的