欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

使用spring-test做spring项目的单元测试,spring-testspring,spring test可

来源: javaer 分享于  点击 48618 次 点评:283

使用spring-test做spring项目的单元测试,spring-testspring,spring test可


spring test可以方便spring项目的单元测试, 使用spring test需要先引用spring test依赖,如下:

        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>3.2.3.RELEASE</version>            <scope>test</scope>        </dependency>

然后可以通过下面方式来做spring类的单元测试:

/** * Created by byrx.net on 2015/8/30. */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"/spring.xml"})public class FirstSpringUnitTest {    @Autowired    private INoteService noteService;    @Test    public void assertDaoIsNoteNull() {        Assert.assertNotNull(noteService);    }}

注意类的两个注解:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"/spring.xml"})

@RunWith指定junit要使用SpringJUnit4ClassRunner来运行单元测试, 第二个注解@ContextConfiguration(locations={"/spring.xml"})指定spring配置文件的位置

相关栏目:

用户点评