被测应用

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

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修饰测试用例
断言是自动化测试的精华也是不可缺少的部分