Junit单元测试与Spring整合

Junit单元测试

Spring整合junit的配置

  • 导入spring整合junit的jar(坐标)
  • 使用Junit提供的一个注解把原有的main方法替换:@RunWith
  • 告知spring的运行器,spring和ioc创建是基于xml还是注解的:@ContextConfiguration
    • locations:指定xml文件的位置
    • classes指定注解类所在的位置
  • 当使用spring 5.x版本的时候,要求junit的jar包必须是4.12及以上

示例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:bean.xml" })
public class Test1 {

    @Autowired
    MyService myService = null;

    @Test
    public void test(){
        myService.findAll();
    }

}
发布了70 篇原创文章 · 获赞 43 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_25884515/article/details/104027818