JUnit测试更新Contact对象失败,请求问题排查协助
问题排查与修复:联系人更新测试失败
首先看失败栈信息,明确核心错误:
java.lang.NullPointerException: Cannot invoke "contact.Contact.getId()" because "this.contact" is null
at contact.ContactService.updateExistingContact(ContactService.java:52)
核心问题汇总
- 空指针异常根源:ContactService类中声明的
Contact contact成员变量从未赋值,updateExistingContact和deleteExistingContact方法错误调用contact.getId(),应该用方法传入的ID参数匹配列表中的联系人。 - 联系人列表初始化错误:ContactService构造函数中重新定义了局部变量
ArrayList<Contact> contacts,导致成员变量的列表未被正确初始化,添加的联系人无法存入列表。 - 更新逻辑错误:
updateExistingContact方法中所有字段更新都调用setFirstName,未对应使用setLastName、setPhone、setAddress,且Contact类缺少这些setter方法。 - 删除方法逻辑错误:
deleteExistingContact方法同样误用未初始化的contact.getId(),且增强for循环中直接remove元素会触发并发修改异常。 - 测试逻辑错误:
testDeleting方法未先添加联系人就执行删除,导致测试结果不符合预期。
修正后的Contact.java
补充缺失的setter方法,并添加参数校验:
package contact; public class Contact { private String id; private String firstName; private String lastName; private String phone; private String address; public Contact(String id, String firstName, String lastName, String phone, String address) { if(id == null || id.length()>10) { throw new IllegalArgumentException("Invalid id"); } if(firstName == null || firstName.length()>10) { throw new IllegalArgumentException("Invalid first name"); } if(lastName == null || lastName.length()>10) { throw new IllegalArgumentException("Invalid last name"); } if(phone == null || phone.length()!=10) { throw new IllegalArgumentException("Invalid phone number"); } if(address == null || address.length()>30) { throw new IllegalArgumentException("Invalid address"); } this.id = id; this.firstName = firstName; this.lastName = lastName; this.phone = phone; this.address = address; } public String getId() { return id; } public void setFirstName(String firstName) { if(firstName == null || firstName.length()>10) { throw new IllegalArgumentException("Invalid first name"); } this.firstName = firstName; } public String getFirstName() { return firstName; } public void setLastName(String lastName) { if(lastName == null || lastName.length()>10) { throw new IllegalArgumentException("Invalid last name"); } this.lastName = lastName; } public String getLastName() { return lastName; } public void setPhone(String phone) { if(phone == null || phone.length()!=10) { throw new IllegalArgumentException("Invalid phone number"); } this.phone = phone; } public String getPhone() { return phone; } public void setAddress(String address) { if(address == null || address.length()>30) { throw new IllegalArgumentException("Invalid address"); } this.address = address; } public String getAddress() { return address; } }
修正后的ContactService.java
移除无用成员变量,修正初始化、删除、更新逻辑:
package contact; import java.util.ArrayList; import java.util.Iterator; public class ContactService { private ArrayList<Contact> contacts = new ArrayList<>(); public ContactService() { // 直接使用成员变量列表,无需重新定义局部变量 } public boolean addNewContact(Contact contact) { boolean isPresent = false; for (Contact contactList : contacts) { if (contactList.getId().equals(contact.getId())) { isPresent = true; break; } } if (!isPresent) { contacts.add(contact); return true; } else { return false; } } public boolean deleteExistingContact(String id) { Iterator<Contact> iterator = contacts.iterator(); while (iterator.hasNext()) { Contact contactList = iterator.next(); if (contactList.getId().equals(id)) { iterator.remove(); return true; } } return false; } public boolean updateExistingContact(String contactID, String firstName, String lastName, String phoneNumber, String address) { for (Contact contactList : contacts) { if (contactList.getId().equals(contactID)) { if (firstName != null && !firstName.isEmpty() && firstName.length() <= 10) { contactList.setFirstName(firstName); } if (lastName != null && !lastName.isEmpty() && lastName.length() <= 10) { contactList.setLastName(lastName); } if (phoneNumber != null && !phoneNumber.isEmpty() && phoneNumber.length() == 10) { contactList.setPhone(phoneNumber); } if (address != null && !address.isEmpty() && address.length() <= 30) { contactList.setAddress(address); } return true; } } return false; } }
修正后的ContactServiceTest.java
完善测试逻辑,添加结果验证:
package test; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import contact.Contact; import contact.ContactService; class ContactServiceTest { @Test void testAdding() { ContactService contactService = new ContactService(); Contact one = new Contact("123456", "Frodo", "Baggins", "2327148686", "42 Shire Street"); Contact two = new Contact("343499", "Anakin", "Skywalker", "4534235612", "68 Tatooine Court"); Contact three = new Contact("585858", "Bart", "Simpson", "6123498999", "42 Shire Street"); assertEquals(true, contactService.addNewContact(one)); assertEquals(true, contactService.addNewContact(two)); assertEquals(true, contactService.addNewContact(three)); // 测试重复ID添加 assertEquals(false, contactService.addNewContact(one)); } @Test void testDeleting() { ContactService contactService = new ContactService(); Contact one = new Contact("123456", "Frodo", "Baggins", "2327148686", "42 Shire Street"); contactService.addNewContact(one); assertEquals(true, contactService.deleteExistingContact("123456")); assertEquals(false, contactService.deleteExistingContact("123456")); } @Test void testUpdating() { ContactService contactService = new ContactService(); Contact two = new Contact("343499", "Anakin", "Skywalker", "4534235612", "68 Tatooine Court"); contactService.addNewContact(two); assertEquals(true, contactService.updateExistingContact("343499", "Luke", "Skywalker", "4534235612", "68 Tatooine Court")); // 验证更新结果 assertEquals("Luke", two.getFirstName()); assertEquals(false, contactService.updateExistingContact("555888", "Luke", "Skywalker", "4534235612", "68 Tatooine Court")); } }
内容的提问来源于stack exchange,提问作者MachoMan
相关产品推荐
相关产品推荐

