springboot junit测试哪些功能_单元测试

一、添加依赖

需要在pom.xml添加依赖和插件,给出的是试验成功过的,可能不是最小集

依赖:

<dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework.restdocs</groupId>
          <artifactId>spring-restdocs-mockmvc</artifactId>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.springframework.security</groupId>
          <artifactId>spring-security-test</artifactId>
          <scope>test</scope>
      </dependency>
      
      <!-- To run tests on IDE such as Eclipse, Intellij -->
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>${junit.version}</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.0.2</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
          <version>${junit.version}.0</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-runner</artifactId>
          <version>1.0.2</version>
          <scope>test</scope>
      </dependency>
      <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-surefire-provider</artifactId>
          <version>1.0.0</version>
      </dependency>

插件: 

<plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19</version>
        </plugin>

二、创建测试类

对要测试的类创建响应的测试类,用于编写测试代码。

可以自己创建,但是推荐鼠标点在类名上,ALT+ENTER快捷键创建。

springboot junit测试哪些功能_单元测试_02

然后弹出下面的创建测试类界面,默认JUnit5,可以选要创建的方法,然后确认。

 

springboot junit测试哪些功能_junit_03

 然后就在test目录下与原文件对应的目录结构下创建了测试类文件。

 

springboot junit测试哪些功能_单元测试_04

springboot junit测试哪些功能_单元测试_05

三、编写测试类 

 这里的自由度很大,你得明白自己要测试什么。

springboot junit测试哪些功能_junit_06

我这里就测试原文件这个方法的返回值的数据库数据条数,是不是我预测的数量。

所以编写测试类如下:

public class TestControllerTest {
    @Autowired
    TestController testController;

    @Test
    public void testTestOpenJdbc() {
        List result = (List)testController.testOpenJdbc();
        Assert.assertEquals(result.size(),5);
    }
}

 另外,还需要在测试类名上加如下注解:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)

这里面是要将TestController这个bean从Spring容器取到,然后调用它的testOpenJdbc()方法的,所以用到了@Autowired注解。

由于运行单元测试的时候,Spring默认不会创建web容器环境,因此,对于javaweb后台类型的项目,需要在@SpringBootTest后添加(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 来启动随机端口的容器运行环境

并且需要用到断言,去判断数据是否是预期的,所以用到的Assert

四、运行测试类

点击测试类或者具体测试方法上的启动按钮,运行测试

springboot junit测试哪些功能_spring_07

 正常执行完成,方法返回值与预期的一致为5,测试通过。

如果把5改为3,则会出现测试失败,运行结果会打个叉叉。

springboot junit测试哪些功能_junit_08

注意点:

1、依赖引入要注意springboot的版本号与springtest是否有冲突,我的spring-boot 2.1.6.RELEASE

2、不要引入其他多余的内容,比如我引入了 spring-boot-test-autoconfigure,就会一直运行出错 

中间配置的过程中出现的一些错误,首先要考虑是否依赖引入问题,版本号不对,包冲突等

例如:

java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils$MethodFilter.and(Lorg/springframework/util/ReflectionUtils$MethodFilter;)Lorg/springframework/util/ReflectionUtils$MethodFilter;

 原因:包引入错误,调整

java.lang.NoClassDefFoundError:org/springframework/test/content/TestContesxtAnnotationUtils

原因:包引入错误,spring test相关版本不一致

处理:注释掉spring-boot-autoconfigure

nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

原因:

这是因为在启动单元测试时,SpringBootTest不会启动服务器,WebSocket自然也就没有启动,但是在代码里又配置了WebSocket,就会出错

解决办法:

启动类加上注解

@RunWith(SpringRunner.class)

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)