题目1:
根据下列流程图编写程序实现相应分析处理并显示结果。返回结果“a=x:”(x为2、3或4);其中变量x、y均须为整型。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断期望结果值和实际返回值是否一致。
被测试代码:
public class TM1 {
public String tm1(int x,int y){
int a = 0;
if ( x>= 80 && y>60){
if (!(x >=90 || y>= 90)){
a = 2;
}
}else if (x <= 70 || y<= 70){
a= 3;
}else {
a = 4;
}
return MessageFormat.format("a={0}",a);
}
}
测试代码:
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
public class TM1Test {
private TM1 tm1 = new TM1();
@Test
public void Test01(){
assertThat(tm1.tm1(80,80), CoreMatchers.equalTo("a=2"));
}
@Test
public void Test02(){
assertThat(tm1.tm1(70,70), CoreMatchers.equalTo("a=3"));
}
@Test
public void Test03(){
assertThat(tm1.tm1(75,75), CoreMatchers.equalTo("a=4"));
}
}
题目2:
根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰年,输出2月份正确天数;如为世纪闰年,输出2月份正确天数;不为闰年输出2月份天数。返回结果格式:“year年month月份的天数是days天。”year、month为传入的值,days为判断得到的天数值。其中变量year、month均须为正整数。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断返回期望结果和实际返回是否一致。
被测试代码:
public class TM2 {
public String tm2(int year, int month) {
if (month > 12) {
return "月份输入不正确";
}
int days;
if (month == 2) {
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
days = 29; // 论年
} else {
days = 28; // 非论年
}
} else if (month == 1 | month == 3 | month == 5 | month == 7 | month == 8 | month == 10 | month == 12) {
days = 31; // 大月
} else {
days = 30; // 小月
}
return year + "年" + month + "月份的天数是" + days + "天";
}
}
测试代码:
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class TM2Test {
private TM2 tm2 = new TM2();
@Test
public void Test01(){
assertThat(tm2.tm2(2000,2),equalTo("2000年2月份的天数是29天"));
}
@Test
public void Test02(){
assertThat(tm2.tm2(1900,2),equalTo("1900年2月份的天数是28天"));
}
@Test
public void Test03(){
assertThat(tm2.tm2(2004,2),equalTo("2004年2月份的天数是29天"));
}
@Test
public void Test04(){
assertThat(tm2.tm2(2001,2),equalTo("2001年2月份的天数是28天"));
}
@Test
public void Test05(){
assertThat(tm2.tm2(2000,6),equalTo("2000年6月份的天数是30天"));
}
@Test
public void Test06(){
assertThat(tm2.tm2(2000,7),equalTo("2000年7月份的天数是31天"));
}
}
题目3:
填写快递单时通常需要确定接收人的姓名、手机号和地址。其中要求手机号是 11 位数字字符,地址为字母开头的 10个(含10)以内字母或字母数字共同组成。填写正确则提示“OK”,否则根据实际情况提示“**不符合要求”(**为手机号或地址),退出。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足判定覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断输出文字期望结果值和实际返回值是否一致。
被测试代码:
public class TM3 {
public String tm3(String phone, String address) {
boolean phoneCheck = phone.matches("[0-9]{11}");
boolean addressCheck = address.matches("^[a-zA-Z][a-zA-Z0-9]{9}");
if (phoneCheck && addressCheck){
return "OK";
}else if (!phoneCheck && addressCheck){
return "手机号不符合要求";
}else if (phoneCheck && !addressCheck){
return "地址不符合要求";
}else {
return "手机号和地址不符合要求";
}
}
}
测试代码:
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
public class TM3Test {
private TM3 tm3 = new TM3();
@Test
public void Test01(){
assertThat(tm3.tm3("13055544446","T123456789"),equalTo("OK"));
}
@Test
public void Test02(){
assertThat(tm3.tm3("1305554444","T123456789"),equalTo("手机号不符合要求"));
}
@Test
public void Test03(){
assertThat(tm3.tm3("13055544446","T12345678"),equalTo("地址不符合要求"));
}
@Test
public void Test04(){
assertThat(tm3.tm3("1305554444","T12345679"),equalTo("手机号和地址不符合要求"));
}
}
题目4:
输入小写的字符串。如字符串前缀为ab开头,则将前缀ab替换为ef并打印出替换后字符串,返回文字“替换前缀后的字符串为:”和替换后字符串值;如后缀为cd并且前缀不为ab,替换字符串中所有cd为gh并打印出替换后字符串,返回文字“替换cd后的字符串为:”和替换后字符串值;否则全部字母大写输出,返回文字“大写字母的字符串为:”和转换后的字符串值。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足条件覆盖测试,测试类使用参数化测试(@Parameters)完成测试。使用assertEquals判断期望结果值和实际返回值是否一致。
被测试代码:
public class TM4 {
public String tm4(String str) {
if (str.matches("ab.*")){
return "替换前缀后的字符串为:".concat(str.replaceFirst("ab","ef"));
}else if (str.matches(".*cd")){
return "替换cd后的字符串为:".concat(str.substring(0,str.length()-2)+"gh");
}else {
return "大写字符的字符串是".concat(str.toUpperCase());
}
}
}
测试代码:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.TestCase.assertEquals;
@RunWith(Parameterized.class)
public class TM4Test {
private TM4 tm4 = new TM4();
private String str;
private String result;
public TM4Test(String str,String result){
this.str = str;
this.result = result;
}
@Parameterized.Parameters
public static Collection parm(){
return Arrays.asList(new Object[][]{
{"abcdef","替换前缀后的字符串为:efcdef"},
{"cdcdcd","替换cd后的字符串为:cdcdgh"},
{"wrwr","大写字符的字符串是WRWR"}
});
}
@Test
public void Test01(){
assertEquals(result,tm4.tm4(str));
}
}