SpringBoot系列教材 (十九)- 其他 - 单元测试

 

步骤1:单元测试
步骤2:先运行,看到效果,再学习
步骤3:模仿和排错
步骤4:基于前面的知识点
步骤5:pom.xml
步骤6:测试类
步骤7:测试

步骤 1 : 单元测试

有时候呢,springboot 里要做单元测试,而不是直接跑起来。 比如 jpa 章节的查询数据,想跑个测试,看看数据库里的数据,那么这里就会讲如何做了

步骤 2 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
运行 TestJPA就可以看到如图所示,在控制台打印了数据库的数据

先运行,看到效果,再学习

步骤 3 : 模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

步骤 4 : 基于前面的知识点

本知识点在 Springboot使用JPA实现完整的增删改查 CRUD和分页 的基础上进行

步骤 5 : pom.xml

1. 修改junit 版本为 4.12

2. 增加 spring-boot-starter-test

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

 

  <groupId>com.how2java</groupId>

  <artifactId>springboot</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <name>springboot</name>

  <description>springboot</description>

  <packaging>war</packaging>

   

    <parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>1.5.9.RELEASE</version>

    </parent>

 

    <dependencies>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-tomcat</artifactId>

             

        </dependency>

         

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

            <scope>test</scope>

        </dependency>

         

        <!-- servlet依赖. -->

        <dependency>

              <groupId>javax.servlet</groupId>

              <artifactId>javax.servlet-api</artifactId>

        </dependency>

              <dependency>

                     <groupId>javax.servlet</groupId>

                     <artifactId>jstl</artifactId>

              </dependency>

        <!-- tomcat的支持.-->

        <dependency>

               <groupId>org.apache.tomcat.embed</groupId>

               <artifactId>tomcat-embed-jasper</artifactId>

                

        </dependency>     

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <optional>true</optional<!-- 这个需要为 true 热部署才有效 -->

        </dependency>

         

        <!-- mysql-->

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <version>5.1.21</version>

        </dependency>

 

        <!-- jpa-->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-data-jpa</artifactId>

        </dependency>        

         

        <!-- springboot test -->

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>

 

    <properties>

        <java.version>1.8</java.version>

    </properties>

 

    <build>

        <plugins>

            <plugin>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>

 

</project>

步骤 6 : 测试类

1. 需要加上2个注解:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)

2. 自动装配 CategoryDAO dao; 以便于使用

3. test 方法加上 @Test 注解,然后就可以使用dao来工作了

4. 运行的时候选择 JUnit Test 方式

测试类

package com.how2java.springboot.test;

 

import java.util.List;

 

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import com.how2java.springboot.Application;

import com.how2java.springboot.dao.CategoryDAO;

import com.how2java.springboot.pojo.Category;

 

@RunWith(SpringRunner.class)

@SpringBootTest(classes = Application.class)

public class TestJPA {

 

    @Autowired CategoryDAO dao;

     

    @Test

    public void test() {

        List<Category> cs=  dao.findAll();

        for (Category c : cs) {

            System.out.println("c.getName():"+ c.getName());

        }

         

    }

}

步骤 7 : 测试

运行 TestJPA 就可以看到如图所示的效果了

测试

关注我,分享更多Java全栈技术
更多Java全栈技术内容,点击了解: https://how2j.cn/k/springboot/springboot-test/1933.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值