spring 测试类的新奇使用

在java开发中,我们经常会使用junit,测试起来非常方便。比如下面这样

@Test
public void queryPaging() throws Exception {
    PageInfo pageInfo=tuserServices.queryPaging(0,2);
    List list=pageInfo.getList();
    for (Tuser tuser : list) {
        System.out.println(tuser);
    }
}

除了单纯使用@Test ,还可以使用一些新写法,比如修改junit 的默认执行类。拿spring 来说,在测试类的开头可以这样写:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
public class TuserServicesTest {
    @Test
    public void queryPaging() throws Exception {        
    }
}

上面的写法意思如下:

@runwith 指定使用的单元测试执行类,不使用这个注解,junit则使用默认的执行类Suite。

@ContextConfiguration 用于指定配置文件路径

之所以这样写,是为了更方便的引用配置文件,不用再new对象了。

因为注解,代码更优美!

你可能感兴趣的:(junit)