目录
0、效果
1、jar
注解方式用到了junit,所以需要在右上角下载:
junit-4.12.jar和hamcrest-all-1.3.jar
2、导入到当前项目中
把jar包导入到项目中,导包办法:右键 project->properties->java build path->libaries->add external jars
3、TestSpring
修改TestSpring, 并运行
1. @RunWith(SpringJUnit4ClassRunner.class)
表示这是一个Spring的测试类
2. @ContextConfiguration("classpath:applicationContext.xml")
定位Spring的配置文件
3. @Autowired
给这个测试类装配Category对象
4. @Test
测试逻辑,打印c对象的名称
package com.how2java.test;
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;
import com.how2java.pojo.Category;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
@Autowired
Category c;
@Test
public void test(){
System.out.println(c.getName());
}
}