Java类分别实现求三个整数中的最大数和最小数,并利用JUnit对其进行单元测试
时间: 2024-05-08 10:16:01 浏览: 206
Java单元测试之JUnit
最大数实现:
```java
public class MaxNumber {
public static int getMax(int a, int b, int c) {
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
return max;
}
}
```
最小数实现:
```java
public class MinNumber {
public static int getMin(int a, int b, int c) {
int min = a;
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
return min;
}
}
```
单元测试实现:
```java
import org.junit.Assert;
import org.junit.Test;
public class NumberTest {
@Test
public void testMaxNumber() {
Assert.assertEquals(3, MaxNumber.getMax(1, 2, 3));
Assert.assertEquals(5, MaxNumber.getMax(3, 5, 4));
Assert.assertEquals(10, MaxNumber.getMax(10, 8, 9));
}
@Test
public void testMinNumber() {
Assert.assertEquals(1, MinNumber.getMin(1, 2, 3));
Assert.assertEquals(3, MinNumber.getMin(3, 5, 4));
Assert.assertEquals(8, MinNumber.getMin(10, 8, 9));
}
}
```
阅读全文