Java程序流程图实现及JUnit测试用例
根据流程图编写程序实现相应处理:
public class Program {
public static String calculateJ1(int x, int y) {
int j = 10 * x - y;
return 'j1=: ' + j;
}
public static String calculateJ2(int x, int y) {
int j = (x - y) * (100000 % 7);
return 'j2=: ' + j;
}
public static String calculateJ3(int x, int y) {
double j = y * Math.log(x + 10);
return 'j3=: ' + j;
}
}
编写测试类:
import org.junit.Test;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
public class ProgramTest {
@Test
public void testCalculateJ1() {
int x = 5;
int y = 3;
String result = Program.calculateJ1(x, y);
assertThat(result, startsWith('j1=: '));
}
@Test
public void testCalculateJ2() {
int x = 8;
int y = 2;
String result = Program.calculateJ2(x, y);
assertThat(result, startsWith('j2=: '));
}
@Test
public void testCalculateJ3() {
int x = 6;
int y = 4;
String result = Program.calculateJ3(x, y);
assertThat(result, startsWith('j3=: '));
}
}
这里只给出了三个最基本的测试方法,每个方法测试了一个计算结果的返回值是否以特定的文字开头。根据流程图的不同路径,可以在测试类中编写更多的测试方法来覆盖更多的情况。
原文地址: https://www.cveoy.top/t/topic/pgYQ 著作权归作者所有。请勿转载和采集!