运用Annotation编写JUnit程序和spring依赖注入

1. 下载junit-4.4.zip jar 包

2.  写测试代码

 
public class AppleTest {
	
	@Test
	public void me(){
		System.out.println("hehe");
	}
	
	@Before
	public void testBefore(){
		System.out.println("before");
	}
	@After
	public void after(){
		System.out.println("after");
	}
}

 可以看到有两点不同Junit3

 a. 类不用继承TestCase

 b. 方法不用test开头

 

 

spring依赖注入

1. 首先在applicationContext.xml中添加命名空间

<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    >

2. 在命名空间下添加,使用annotation的声明

<context:annotation-config />

 

3.

public class BizImpl implements IBiz {
	
        @Autowired       
        private IDao dao;
	
	public void check(String uname, String upass) {
		dao.check(uname, upass);
	}
	public IDao getDao() {
		return dao;
	}
	public void setDao(IDao dao) {
		this.dao = dao;
	}
}
 然后框架会自动在spring的配置文件中找bean id 为dao的bean对象

你可能感兴趣的:(DAO,spring,bean,框架,JUnit)