spring使用junit单元测试
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
@RunWith(SpringJUnit4ClassRunner.class) //由SpringJUnit4ClassRunner启动测试 @ContextConfiguration("classpath:applicationContext.xml") //spring的配置文件位置 public class SpringTest{//当前测试类也会被纳入工厂中,所以其中属性可以注入 @Autowired // 注入要测试的组件 @Qualifier("userDAO") private UserDAO userDAO; @Test public void test(){ // 测试使用userDAO userDAO.queryUser(); .... } }