6.19(junit-->在maven和Spring中的使用)

写文章要不忘初心,今天也要继续努力~

白盒测试:是一种测试用例设计方法,在这里盒子指的是被测试的软件,白盒,顾名思义即盒子是可视的,你可以清楚盒子内部的东西以及里面是如何运作的,因此白盒测试需要你对系统内部的结构和工作原理有一个清楚的了解,并且基于这个知识来设计你的用例。

白盒测试主要是单元测试,不是测试岗就不深究了

用main方法测试

//首先用main方法来测试
public class SumUtil {
    public static void main(String[] args) {
        int result = sum1(1,2);
        System.out.println(result);
        int result2 = sum2(1,2,3);
        System.out.println(result2);
    }
    public static int sum1(int i,int j){
        return i+j;
    }
    public static int sum2(int i,int j,int k){
        return i+j+k;
    }
    //原先的测试很繁琐,还有可能破坏原来的测试逻辑


    }

junit测试

import org.junit.Assert;
import org.junit.Test;

public class TestCase1 {

    @Test
    public void testSum1() {
        int result = SumUtil.sum1(1, 2);
        Assert.assertEquals(result, 3);

    }
}


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import junit.framework.Assert;

public class TestCase2 {

    @Before
    public void before() {
        System.out.println("测试前的准备工作,比如链接数据库等等");
    }
    @After
    public void after() {
        System.out.println("测试结束后的工作,比如关闭链接等等");
    }

    @Test
    public void testSum1() {
        int result = SumUtil.sum1(1, 2);
        Assert.assertEquals(result, 3);
    }

    @Test
    public void testSum2() {
        int result = SumUtil.sum2(1, 2,3);
        Assert.assertEquals(result, 6);
    }
}


import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestCase1.class,TestCase2.class})
public class TestSuite {

}

测试结果:
在这里插入图片描述
在这里插入图片描述


在maven中使用,只要在pom.xml中添加依赖就可

<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>cn.how2j</groupId>
  <artifactId>junit</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>junit</name>
  <description>junit</description>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.3.1</version>
        <scope>test</scope>
    </dependency>
  </dependencies>
</project>

在Spring中使用

先导入jar包

修改TestSpring, 并运行

  1. @RunWith(SpringJUnit4ClassRunner.class)
    表示这是一个Spring的测试类

  2. @ContextConfiguration(“classpath:applicationContext.xml”)
    定位Spring的配置文件

  3. @Autowired
    给这个测试类装配Category对象

  4. @Test
    测试逻辑,打印c对象的名称

package com.how2java.test;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import com.how2java.pojo.Category;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpring {
    @Autowired
    Category c;
 
    @Test
    public void test(){
        System.out.println(c.getName());
    }
}

在SpringBoot中使用
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>

测试类

  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());
        }
         
    }
}

唯有不争,举世间莫能与之争!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<template ref="aaa" #就业> <view class="both"> <view class="mb20 mt20" style="font-weight: bold;font-size: 30rpx;">就业行业分布</view> <view class="frame"> <view class="frame-f">医疗保健</view> <tui-progress :percent="30.46" show-info></tui-progress> <view class="frame-f">医药</view> <tui-progress :percent="24.62" show-info></tui-progress> <view class="frame-f">医疗设备</view> <tui-progress :percent="10.29" show-info></tui-progress> <view class="frame-f">快消</view> <tui-progress :percent="6.19" show-info></tui-progress> <view class="frame-f">其他行业</view> <tui-progress :percent="18.83" show-info></tui-progress> </view> <view class="mb20 mt20" style="font-weight: bold;font-size: 30rpx;">岗位去向</view> <tui-charts-pie ref="aaa" diam="250" type="2" @click="onClick"></tui-charts-pie> <view class="mb20 mt20" style="font-weight: bold;font-size: 30rpx;">地区分布</view> <view class="frame"> <view class="frame-f">北京市</view> <tui-progress :percent="30.46" show-info></tui-progress> <view class="frame-f">华地区</view> <tui-progress :percent="24.62" show-info></tui-progress> <view class="frame-f">深圳市</view> <tui-progress :percent="10.29" show-info></tui-progress> <view class="frame-f">西南地区</view> <tui-progress :percent="6.19" show-info></tui-progress> <view class="frame-f">广州市</view> <tui-progress :percent="18.83" show-info></tui-progress> </view> </view> </template> </gs-tabs>如何用uniapp的vue3获取上述代码插槽的ref
06-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值