第一个问题:根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰年,输出2月份正确天数;如为世纪闰年,输出2月份正确天数;不为闰年输出2月份天数。返回结果格式:“year年month月份的天数是days天。”year、month为传入的值,days为判断得到的天数值。其中变量year、month均须为正整数。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断返回期望结果和实际返回是否一致。
第二个问题:邮箱注册需要填写邮箱地址和密码。其中要求邮箱格式“登录名@主机名.域名”,登录名为5个字母,主机名固定为下面2个163、126,域名为com或com.cn。密码为6个(含6)以上数字组成。填写正确则提示“信息正确”,否则根据实际情况提示“不符合要求”(为邮箱地址或密码)。编写程序代码,使用JUnit框架编写测试类对编写的程序代码进行测试,测试类中设计最少的测试数据满足语句覆盖测试,每条测试数据需要在测试类中编写一个测试方法。使用assertThat中equalTo断言判断输出文字期望结果值和实际返回值是否一致。
求以上两个问题的详细代码以及相应注解
单元测试代码怎么实现
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
阿里嘎多学长 2024-07-14 13:33关注以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:
以下是针对您提出两个问题的解决方案,包括程序代码和JUnit测试类代码。
第一个问题:月份天数判断程序
程序代码:
public class MonthDaysCalculator { public String getMonthDays(int year, int month) { if (month < 1 || month > 12) { return "月份输入不正确。"; } switch (month) { case 2: if (isLeapYear(year)) { return String.format("%d年%d月份的天数是29天。", year, month); } else { return String.format("%d年%d月份的天数是28天。", year, month); } case 4: case 6: case 9: case 11: return String.format("%d年%d月份的天数是30天。", year, month); default: return String.format("%d年%d月份的天数是31天。", year, month); } } private boolean isLeapYear(int year) { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } }JUnit测试类:
import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class MonthDaysCalculatorTest { @Test public void testNonLeapYearFebruary() { MonthDaysCalculator calculator = new MonthDaysCalculator(); assertThat(calculator.getMonthDays(2021, 2)).isEqualTo("2021年2月份的天数是28天。"); } @Test public void testLeapYearFebruary() { MonthDaysCalculator calculator = new MonthDaysCalculator(); assertThat(calculator.getMonthDays(2020, 2)).isEqualTo("2020年2月份的天数是29天。"); } @Test public void testInvalidMonth() { MonthDaysCalculator calculator = new MonthDaysCalculator(); assertThat(calculator.getMonthDays(2021, 13)).isEqualTo("月份输入不正确。"); } // 可以继续添加其他月份的测试用例 }第二个问题:邮箱注册程序
程序代码:
public class EmailRegistration { public String validateEmailAndPassword(String email, String password) { if (!isValidEmail(email)) { return "邮箱地址不符合要求"; } if (!isValidPassword(password)) { return "密码不符合要求"; } return "信息正确"; } private boolean isValidEmail(String email) { String[] parts = email.split("@"); if (parts.length != 2) { return false; } String username = parts[0]; String domain = parts[1].split("\\.")[0]; String domainSuffix = parts[1].split("\\.")[1]; return username.length() == 5 && ("163".equals(domain) || "126".equals(domain)) && ("com".equals(domainSuffix) || "com.cn".equals(domainSuffix)); } private boolean isValidPassword(String password) { return password.matches("\\d{6,}"); } }JUnit测试类:
import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; public class EmailRegistrationTest { @Test public void testValidEmailAndPassword() { EmailRegistration registration = new EmailRegistration(); assertThat(registration.validateEmailAndPassword("abcde@163.com", "123456")).isEqualTo("信息正确"); } @Test public void testInvalidEmailUsername() { EmailRegistration registration = new EmailRegistration(); assertThat(registration.validateEmailAndPassword("abcd@163.com", "123456")).isEqualTo("邮箱地址不符合要求"); } @Test public void testInvalidEmailDomain() { EmailRegistration registration = new EmailRegistration(); assertThat(registration.validateEmailAndPassword("abcde@example.com", "123456")).isEqualTo("邮箱地址不符合要求"); } @Test public void testInvalidPasswordLength() { EmailRegistration registration = new EmailRegistration(); assertThat(registration.validateEmailAndPassword("abcde@163.com", "12345")).isEqualTo("密码不符合要求"); } // 可以继续添加其他情况的测试用例 }请注意,上述代码仅为示例,实际应用中可能需要进一步的异常处理和验证逻辑。同时,JUnit测试类中的测试方法应根据实际代码逻辑进行调整,以确保覆盖所有可能的情况。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录