通过junit测试spring框架构建下的Dao层示例:
package com.project.dao;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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;
@RunWith(SpringJUnit4ClassRunner.class) // 不能是PowerMock等别的class,否则无法识别spring的配置文件
@ContextConfiguration("file:conf/applicationContext.xml") // 读取spring配置文件
public class userDAOTest {
@Autowired //检索applicationContext.xml,自动注入指定的bean
UserDAO userDAO;
@Test
public void testGetUser() {
int userId = 1;
String nickname = "昵称1";
Assert.assertEquals(userId, userDAO.getUser( userId ).getUserId());
Assert.assertEquals(nickname, userDAO.getUser( userId ).getNickname());
}
}