Spring3.0结合JUnit4进行单元测试

当运用Junit4进行单元测试时,可以使用@ContextConfiguration注解读取配置文件,然后使用@Resource注解引入Bean,完成单元测试。

①Junit代码:

import javax.annotation.Resource;
import org.junit.Test;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.test.model.User;

@ContextConfiguration("classpath:beans.xml")//对应src目录下的beans.xml
public class UserDAOTest extends AbstractJUnit4SpringContextTests{
	
	@Resource(name="userDAO")
	private UserDAO userDAO;
	
	@Test
	public void testSave() {
	//TODO
		this.userDAO.save(new User());
	}

}

②beans.xml的配置




	
	
	


你可能感兴趣的:(Spring)