【测试】spring与Junit整合的单元测试

一、导入jar

        <!--Junit3默认编程的方式,junit4注解-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
          <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>

二、新建Test文件夹

java右键,Mark Directory as ----->Test Source Root

三、新建测试类

1.在需要测试的类中 Ctrl+Shift+T,创建测试类,别忘记选择JUnit4

2.在测试类前,配置需要的配置文件(我此处只测试dao层,所以需要告诉Junit spring-dao文件所在位置,为了junit启动时加载springIOC容器),如果不需要可以不配置

@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring 配置文件
@ContextConfiguration({"classpath:config/spring-dao.xml"})

3.编写测试方法,运行即可

猜你喜欢

转载自blog.csdn.net/heni6560/article/details/83382075