SpringBoot单元测试

导读:本篇文章讲解 SpringBoot单元测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

不同版本的单元测试方式还不一样,这里做一个总结。

Springboot 1.5.x版本

主要Junit和springboot的版本要一致,否则报错。

@Slf4j
// 必须要设置classes,不然不知道是对那个项目测试,会报错
@SpringBootTest(classes = Application.class)
// 低版本springboot需要添加@RunnWith,否则无法自动注入spring对象,springboot 2.5.0不用添加@RunnWith
// 主要版本,springboot1.5.x 对应spring 4.x, spring 4.x使用junit 4.x
@RunWith(SpringRunner.class)
public class InterfaceTest {
    @Test
    public void test1() {
        System.out.println("sucess");
    }
}

pom.xml配置

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.5.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/100260.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!