赞
踩
题目2:根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰年,输出2月份正确天数;如为世纪闰年,输出2月份正确天数;不为闰年输出2月份天数。返回结果格式:“year年month月份的天数是days天。”year、month为传入的值,days为判断得到的天数值。其中变量year、month均须为正整数。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断返回期望结果和实际返回是否一致。
- import org.junit.Test;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.equalTo;
-
- public class MonthDaysTest {
-
- public int getMonthDays(int year, int month) {
- if (month < 1 || month > 12) {
- throw new IllegalArgumentException("月份输入不正确。");
- }
-
- int days;
- if (month == 2) {
- if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
- days = 29; // 闰年
- } else {
- days = 28; // 非闰年
- }
- } else if (month == 4 || month == 6 || month == 9 || month == 11) {
- days = 30;
- } else {
- days = 31;
- }
-
- return days;
- }
-
- @Test
- public void testGetMonthDays() {
- MonthDaysTest monthDaysTester = new MonthDaysTest();
-
- assertThat(monthDaysTester.getMonthDays(2023, 6), equalTo(30));
- assertThat(monthDaysTester.getMonthDays(2023, 2), equalTo(28));
- assertThat(monthDaysTester.getMonthDays(2024, 2), equalTo(29));
- assertThat(monthDaysTester.getMonthDays(2023, 13), equalTo(null));
- // 其他测试数据...
- }
- }
题目3:填写快递单时通常需要确定接收人的姓名、手机号和地址。其中要求手机号是 11 位数字字符,地址为字母开头的 10个(含10)以内字母或字母数字共同组成。填写正确则提示“OK”,否则根据实际情况提示“**不符合要求”(**为手机号或地址),退出。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足判定覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断输出文字期望结果值和实际返回值是否一致。
- import org.junit.Test;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.equalTo;
-
- public class DeliveryFormTest {
-
- public String validateForm(String name, String phoneNumber, String address) {
- if (!phoneNumber.matches("\\d{11}")) {
- return "手机号不符合要求";
- }
-
- if (!address.matches("[a-zA-Z0-9]{1,10}")) {
- return "地址不符合要求";
- }
-
- // 填写正确
- return "OK";
- }
-
- @Test
- public void testValidateForm() {
- DeliveryFormTest formTester = new DeliveryFormTest();
-
- assertThat(formTester.validateForm("John", "12345678900", "Apt 123"), equalTo("OK"));
- assertThat(formTester.validateForm("Alice", "1234", "Apt 456"), equalTo("手机号不符合要求"));
- assertThat(formTester.validateForm("Bob", "12345678900", "This is a very long address"), equalTo("地址不符合要求"));
- // 其他测试数据...
- }
- }
题目4:输入小写的字符串。如字符串前缀为ab开头,则将前缀ab替换为ef并打印出替换后字符串,返回文字“替换前缀后的字符串为:”和替换后字符串值;如后缀为cd并且前缀不为ab,替换字符串中所有cd为gh并打印出替换后字符串,返回文字“替换cd后的字符串为:”和替换后字符串值;否则全部字母大写输出,返回文字“大写字母的字符串为:”和转换后的字符串值。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足条件覆盖测试,测试类使用参数化测试(@Parameters)完成测试。使用assertEquals判断期望结果值和实际返回值是否一致。
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.junit.runners.Parameterized;
- import static org.junit.Assert.assertEquals;
-
- import java.util.Arrays;
- import java.util.Collection;
-
- @RunWith(Parameterized.class)
- public class StringTransformationTest {
-
- @Parameterized.Parameter(0)
- public String input;
-
- @Parameterized.Parameter(1)
- public String expectedOutput;
-
- @Parameterized.Parameters
- public static Collection<Object[]> data() {
- return Arrays.asList(new Object[][] {
- {"abcd", "efgh"}, // 替换前缀
- {"xyzcdxyz", "xyzghxyz"}, // 替换后缀
- {"HelloWorld", "HELLOWORLD"}, // 大写字母
- {"", ""} // 空字符串
- });
- }
-
- public String transformString(String input) {
- if (input.startsWith("ab")) {
- return "替换前缀后的字符串为: " + input.replaceFirst("ab", "ef");
- } else if (input.endsWith("cd") && !input.startsWith("ab")) {
- return "替换cd后的字符串为: " + input.replaceAll("cd", "gh");
- } else {
- return "大写字母的字符串为: " + input.toUpperCase();
- }
- }
-
- @Test
- public void testTransformString() {
- assertEquals(expectedOutput, transformString(input));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。