SpringBoot

扫码查看

比如在创建项目时默认的版本为2.2.2版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

然后我们修改为1.5.10的低版本:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

在高版本时,默认的测试类是没可以使用的

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootTestWebApplicationTests {

    @Test
    void contextLoads() {
        System.out.println("hello world");
    }

}

在更换成低版本之后,测试类将会报错,如下所示:

 此时可以做如下修改

1、删除它默认导入的org.junit.jupiter.api.Test类,重新导入org.junit.Test类

2、在类上添加注释@RunWith(SpringRunner.class),如下图:

3、将测试类和测试方法都修改为public

最后修改的测试类如下所示:

package com.susu.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTestApplicationTests {

    @Test
    public void contextLoads() {
        System.out.println("hello world");
    }

}

运行结果:

12-15 14:13
查看更多