-3

I'm working on a school project and I have the JUnit test almost here but I cannot for the life of me get this to work.

Contact looks like:

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 and ContactServiceTest look like;

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;
    }
}

ContactServiceTest


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));
    }

}

I would appreciate any help on this as I'm completely lost and I've gotten no help, sorry for the code dump I just wanted you to have a full picture of what's going on if something -or everything- is wrong.

  • Are you saying that your test fails? What's your issue? – Marco Tizzano Mar 18 '21 at 00:16
  • I have never used jupiter test classes, first off I would suggest that you use just org.junit classes. Then, we can go step by step with your code and make it work properly – Marco Tizzano Mar 18 '21 at 00:19
  • It's that it's erroring as assertTrue(cs.addContact(contact)) shows up as ambiguous. Any help for why this is happening would be appreciated and how to solve this. – There'saBeeInMyHouse Mar 18 '21 at 00:33

1 Answers1

1

The "ambigous" error is because the compiler cannot determine whether you are trying to invoke the assertTrue method of class org.junit.Assert or the assertTrue method of org.junit.jupiter.api.Assertions.

To fix this, just remove the following statement:

import static org.junit.Assert.*;

Moreover, here are some tips to improve your code:

  1. Remove the comment of the equals method in Contact class, that method is fine. In addition, you can make Contact class implement the interface java.lang.Comparable and implement the method public int compareTo(Object o), which will be based on what you did for equals, but invoking compareTo instead of equals for each instance member of Contact.
  2. If you follow the fist point, in your ContactService you don't need to visit your ArrayList of contact to verify the existance of an element, that is not necessary.
  3. If you don't want to have duplicated elements in your list, Java gives you the possibility to choose another collection, which is made for the purpose (i.e. TreeSet).

Here is a revisited version of your 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;
    }
}

In addition, here is another version of ContactServiceTest, where I just added a couple of more scenarios. Starting from this, you can go on and implement more tests, for example related to 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));
    }

}
Marco Tizzano
  • 1,726
  • 1
  • 11
  • 18