Spring——集成Junit

简介
  Spring结合Junit测试只能在test包下!
  原理:通过@ContextConfiguration()给test注入applicationContext。通过注解注入获取对象


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringCofiguration.class})
// @ContextConfiguration("classpath:applicationContext.xml")
// 需要的对象直接依赖注入。即可调用对象。 (对象需要存在spring容器中)
public class SpringJunitTest {

@Autowired //依赖注入
private UserService userService;

@Autowired //依赖注入
private ComboPooledDataSource comboPooledDataSource;

@Test
public void test1(){
userService.save();
}

@Test
public void test2() throws SQLException {
Connection con = comboPooledDataSource.getConnection();
System.out.println(con);
}
}
posted @ 2021-11-16 20:39  remix_alone  阅读(21)  评论(0)    收藏  举报