单元测试系列[v1.0.0][Junit5]

本文介绍了一个像素密度(PPI)计算器的实现方法,通过输入屏幕宽度、高度及尺寸来计算像素密度。文章提供了Java代码示例,并详细说明了计算逻辑及异常处理。

被测应用

像素密度计算器,用于当用户输入屏幕的宽度、高度和尺寸后,计算像素密度

public class CalculatorForPpi {
    public static long calculate(int width, int height, double size){
        long result;
        if (width > 0 && height > 0 && size>0){
           result = Math.round(Math.pow((Math.pow(width, 2) + Math.pow(height, 2)) / Math.pow(size, 2), 0.5));
        }else {
            result = -1;
        }
        return result;
    }
}

入参分析

width为屏幕宽度,height为屏幕高度,都属于int型;size为屏幕尺寸属于double型,返回像素密度为long型;如果宽度、高度、屏幕尺寸任意一个小于或等于0则返回-1,如果大于0则正常计算,计算公式为宽的平方加上高的平方:PPI= 宽 2 + 高 2 / 尺寸 \sqrt{\def\foo{宽^2+高^2} \foo}/尺寸 2+2 /尺寸

参数设置

  • Case1: width=750 height=1334 size=4.7 计算像素密度
  • Case1: width=-1 height=1334 size=4.7 不计算像素密度
  • Case1: width=0 height=1334 size=4.7 不计算像素密度
  • Case1: width=750 height=-1 size=4.7 不计算像素密度
  • Case1: width=750 height=0 size=4.7 不计算像素密度
  • Case1: width=750 height=1334 size=-1 不计算像素密度
  • Case1: width=750 height=1334 size=0 不计算像素密度

POM配置

   <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>

测试用例

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class CalculatorForPpiTest {
    private static int height;
    private static int width;
    private static double size;


    @BeforeAll
    static void init(){
        width = 750;
        height = 1334;
        size = 4.7;
    }

    @Test
    void testCase1(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(width, height, size));
    }

    @Test
    void testCase2(){
        Assertions.assertEquals(-1, CalculatorForPpi.calculate(-1, height, size));
    }

    @Test
    void testCase3(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(0, height, size));
    }

    @Test
    void testCase4(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(width, -1, size));
    }

    @Test
    void testCase5(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(width, 0, size));
    }

    @Test
    void testCase6(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(width, height, -1));
    }

    @Test
    void testCase7(){
        Assertions.assertEquals(326, CalculatorForPpi.calculate(width, height, 0));
    }
}

说明

init方法被@BeforeAll修饰,是整个Class的初始化操作
Junit使用@Test修饰测试用例
断言是自动化测试的精华也是不可缺少的部分

更多应用可以查看Junit5官方文档

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Davieyang.D.Y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值