1,在eclipse中安裝TestNg插件,這里省略。。
2,編寫測(cè)試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"})這個(gè)注解是的localtion屬性使用的是src類路徑下面的