首页 > 编程语言 > 详细

Spring整合Junit4进行单元测试

时间:2020-01-29 09:42:40      阅读:69      评论:0      收藏:0      [点我收藏+]

 

需要添加spring-text.RELEASE.jar。

 

需要添加Junit4的2个jar包:junit.jar、hamcrest-core.jar。

 

 

示例

@RunWith(SpringJUnit4ClassRunner.class)
//值是String数组,可以写多个xml配置文件
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
public class Test {
    @Autowired
    private User user;

    @org.junit.Test
    public void test1(){
        System.out.println(user);
    }
    
    @org.junit.Test
    public void test2(){
        System.out.println(user);
    }
    
    @org.junit.Test
    public void test3(){
        System.out.println(user);
    }
}

 

 

在测试类上标注:

  • @RunWith  会自动根据配置创建Spring容器,无需 new ClassPathXmlApplicationContext("spring-config.xml")  手动创建

  • @ContextConfiguration  指定xml配置文件的路径

 

 

在测试方法上标注:

  • @Test

 

 

可添加前后处理:

    @Before
    public void before(){
        System.out.println("before");
    }

    @After
    public void after(){
        System.out.println("after");
    }

 

 

run/debug时使用Junit:

技术分享图片

 

Spring整合Junit4进行单元测试

原文:https://www.cnblogs.com/chy18883701161/p/12239582.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!