首页 > 解决方案 > 模糊联系服务的 Java JUnit 问题

问题描述

我正在做一个学校项目,我几乎在这里进行了 JUnit 测试,但我一辈子都无法让它发挥作用。

联系方式如下:

public class Contact {

    private String contactID;
    private String firstName;
    private String lastName;
    private String phoneNumber;
    private String address;
    
    public Contact (String contactID, String firstName, String lastName, String phoneNumber, String address)
    {
        //ContactTests Tests
        //test to see if contact ID is NOT null or above 10 characters
        if (contactID == null || contactID.length()>10)
        {
            throw new IllegalArgumentException("Invalid contact ID");
            //not updating because says do not update it
        }
        //test to see if first name is NOT null or above 10 characters
        if (firstName == null || firstName.length()>10)
        {
            throw new IllegalArgumentException("Invalid first name");
        }
        if (lastName == null || lastName.length()>10)
        {
            throw new IllegalArgumentException("Invalid last name");
        }
        //test to see if phone number is NOT null or not 10 characters
        if (phoneNumber == null || phoneNumber.length()>10 || phoneNumber.length()<10)
        {
            throw new IllegalArgumentException("Invalid phone number");
        }
        //test to see if address is NOT null or more than 30 characters
        if (address == null || address.length()>30)
        {
            throw new IllegalArgumentException("Invalid address");
        }
        
        //apply them
        this.contactID = contactID;
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
        this.address = address;
        
    }
    
    //return the variables
    //used within tests
    
    public String getContactID() {
        return contactID;
    }
    
    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public String getAddress() {
        return address;
    }

    
    //for contactService need to create values here to get variables to set into the array
    //used for First Name, Last Name, Phone Number, Address
    //used within ContactServices
    
    public void setFirstName (String firstName)
    {
        this.firstName = firstName;
    }
    
    public void setLastName (String lastName)
    {
        this.lastName = lastName;
    }
    
    public void setPhoneNumber (String phoneNumber)
    {
        this.phoneNumber = phoneNumber;
    }
    
    public void setAddress (String address)
    {
        this.address = address;
    }
    
    //test if two contacts are the same
    //used in contactService and contactServiceTest
    //geeksforgeeks.org/overriding-equals-method-in-java/ helped me in this idea of changing equals
    //to becoming more complex
    /*@Override
    public boolean equals(Object test)
    {
        if (this == test)
        {
            return true;
        }
        if ((getClass() != test.getClass()) || (test == null))
        {
            return false;
        }
        Contact another = (Contact) test;
        if (contactID == null)
        {
            if (another.contactID != null || (!contactID.equals(another.contactID)))
            {
                return false;
            }
        }
        if (firstName == null)
        {
            if (another.firstName != null || (!firstName.equals(another.firstName)))
            {
                return false;
            }
        }
        if (lastName == null)
        {
            if (another.lastName != null || (!lastName.equals(another.lastName)))
            {
                return false;
            }
        }
        if (phoneNumber == null)
        {
            if (another.phoneNumber != null || (!phoneNumber.equals(another.phoneNumber)))
            {
                return false;
            }
        }
        if (address == null)
        {
            if (another.address != null || (!address.equals(another.address)))
            {
                return false;
            }
        }
        return true;
    }*/
    
}

ContactService 和 ContactServiceTest 看起来像;

import java.util.ArrayList;
import contact.Contact;


public class ContactService {
    
    //will contain our list of contacts
    //list was removed as working solely with array list now
    private ArrayList<Contact> contacts;
    
    public ContactService()
    {
        //beginning call for the array list
        contacts = new ArrayList<>();
    }
    
    //need to have an add contact, remove contact and update contact feature
    
    
    //set add contact to have all values
    public boolean addContact(Contact contact)
    {
        boolean contactAlready = false;
        //run through all the contacts in the list made
        for (Contact contactList:contacts)
        {
            //test to see if already a contact
            //if so make contactAlready true
            if (contactList.equals(contact))
            {
                contactAlready = true;
            }
        }
        //if not a contact add it as one
        if (!contactAlready)
        {
            contacts.add(contact);
            //after adding is now true
            return true;
        }
        else
        {
            //ending false statement
            return false;
        }
    }
    
    //delete needed via contactID
    public boolean deleteContact(String contactID)
    {
        //run through list of contacts
        for (Contact contactList:contacts)
        {
            //if equals to contactID will remove and return
            if (contactList.getContactID().equals(contactID))
            {
                //remove and return true
                contacts.remove(contactList);
                return true;
            }
        }
        //else fail and return false
        return false;
    }
    
    //update is trickiest due to needing to make sure still fits parameters
    //"" means no change
    public boolean updateContact(String contactID, String firstName, String lastName, String phoneNumber, String address)
    {
        //run through loop again
        for (Contact contactList:contacts)
        {
            //if contactID matches, run through each with making sure not "" and meets requirements
            //then return true as it did equal update.
            if (contactList.getContactID().equals(contactID))
            {
                //set each of the values as long as meet's requirements nor empty
                if(!firstName.equals("") && !(firstName.length()>10))
                {
                    contactList.setFirstName(firstName);
                }
                if(!lastName.equals("") && !(lastName.length()>10))
                {
                    contactList.setFirstName(lastName);
                }
                if(!phoneNumber.equals("") && (phoneNumber.length()==10))
                {
                    contactList.setFirstName(phoneNumber);
                }
                if(!address.equals("") && !(address.length()>30))
                {
                    contactList.setFirstName(address);
                }
                //return true as did update
                return true;        
            }
        }
        //else fail and return false
        return false;
    }
}

联系服务测试


import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;

import contact.Contact;
import contact.ContactService;

class ContactServiceTest {

    //need to test add, delete and update
    //templates
    /*
     * Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        Contact("1309403", "Malleus", "Draconia", "2187123404", "Valley of Thorns");
        Contact("9752319", "Vil", "Schoenheit", "9215501793", "Land of Proxynee");
     */
    
    @Test
    public void testAdd()
    {
        ContactService cs = new ContactService();
        Contact contact = new Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        assertTrue(cs.addContact(contact));
        //assertEquals(true, cs.addContact(contact));
    }

}

我会很感激任何帮助,因为我完全迷失了,我没有得到任何帮助,对不起代码转储我只是想让你全面了解如果某事 - 或一切 - 是错误的会发生什么。

标签: javajunitambiguous

解决方案


“模棱两可”的错误是因为编译器无法确定您是在尝试调用assertTrueclassorg.junit.AssertassertTrue方法还是org.junit.jupiter.api.Assertions.

要解决此问题,只需删除以下语句:

import static org.junit.Assert.*;

此外,这里有一些改进代码的技巧:

  1. equals去掉类中方法的注释Contact,那个方法就好了。此外,您可以让Contact类实现接口java.lang.Comparable并实现方法,public int compareTo(Object o)这将基于您为.equalscompareToequalsContact
  2. 如果您遵循第一个点,则在您ContactService的联系人中您不需要访问您的 ArrayList 来验证元素的存在,那是没有必要的。
  3. 如果您不想在列表中包含重复的元素,Java 让您可以选择另一个集合,该集合是为此目的而创建的(即 TreeSet)。

这是您的重新访问版本ContactService

package contact;

import java.util.ArrayList;


public class ContactService {

    //will contain our list of contacts
    //list was removed as working solely with array list now
    private List<Contact> contacts;

    public ContactService()
    {
        //beginning call for the array list
        contacts = new ArrayList<>();
    }

    //need to have an add contact, remove contact and update contact feature


    //set add contact to have all values
    public boolean addContact(Contact contact)
    {
        if(!contacts.contains(contact)) {
            return contacts.add(contact);
        }

        return false;
    }

    //delete needed via contactID
    public boolean deleteContact(String contactID)
    {
        //run through list of contacts
        for (Contact contactElement : contacts)
        {
            //if equals to contactID will remove and return
            if (contactElement.getContactID().equals(contactID))
            {
                return contacts.remove(contactElement);
            }
        }
        //else fail and return false
        return false;
    }

    //update is trickiest due to needing to make sure still fits parameters
    //"" means no change
    public boolean updateContact(String contactID, String firstName, String lastName, String phoneNumber, String address)
    {
        //run through loop again
        for (Contact contactList:contacts)
        {
            //if contactID matches, run through each with making sure not "" and meets requirements
            //then return true as it did equal update.
            if (contactList.getContactID().equals(contactID))
            {
                //set each of the values as long as meet's requirements nor empty
                if(!firstName.equals("") && !(firstName.length()>10))
                {
                    contactList.setFirstName(firstName);
                }
                if(!lastName.equals("") && !(lastName.length()>10))
                {
                    contactList.setFirstName(lastName);
                }
                if(!phoneNumber.equals("") && (phoneNumber.length()==10))
                {
                    contactList.setFirstName(phoneNumber);
                }
                if(!address.equals("") && !(address.length()>30))
                {
                    contactList.setFirstName(address);
                }
                //return true as did update
                return true;
            }
        }
        //else fail and return false
        return false;
    }
}

此外,这里是另一个版本ContactServiceTest,我只是在其中添加了几个场景。从这里开始,您可以继续并实施更多测试,例如与 updateContact 相关的测试。

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import contact.Contact;
import contact.ContactService;

class ContactServiceTest {

    //need to test add, delete and update
    //templates
    /*
     * Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        Contact("1309403", "Malleus", "Draconia", "2187123404", "Valley of Thorns");
        Contact("9752319", "Vil", "Schoenheit", "9215501793", "Land of Proxynee");
     */

    @Test
    public void testAdd()
    {
        ContactService cs = new ContactService();
        Contact contact = new Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        assertTrue(cs.addContact(contact));

        contact = new Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        //assertEquals(true, cs.addContact(contact));

        assertFalse(cs.addContact(contact));
    }

    @Test
    public void testRemove()
    {
        ContactService cs = new ContactService();
        Contact contact = new Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
        cs.addContact(contact);

        assertTrue(cs.deleteContact("1413252"));
        //assertEquals(true, cs.addContact(contact));
    }

}

推荐阅读