Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有不明确contactService的Java JUnit问题_Java_Junit_Ambiguous - Fatal编程技术网

带有不明确contactService的Java JUnit问题

带有不明确contactService的Java JUnit问题,java,junit,ambiguous,Java,Junit,Ambiguous,我正在做一个学校的项目,我的JUnit考试就快到了,但我一辈子都无法让它发挥作用 联系人看起来像: public class Contact { private String contactID; private String firstName; private String lastName; private String phoneNumber; private String address; public Contact (St

我正在做一个学校的项目,我的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;
    }*/
    
}

我很感激能在这方面得到任何帮助,因为我完全迷路了,也没有得到任何帮助,对于代码转储,我很抱歉,我只是想让您全面了解如果某件事情或所有事情都出错了会发生什么。

模棱两可的错误是因为编译器无法确定您是尝试调用类org.junit.Assert的assertTrue方法还是org.junit.jupiter.api.Assertions的assertTrue方法

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

import static org.junit.Assert.*;
此外,以下是一些改进代码的提示:

删除Contact类中equals方法的注释,该方法很好。此外,您可以使Contact类实现接口java.lang.Comparable并实现方法public int compareToObject o,该方法将基于您对equals所做的操作,但对Contact的每个实例成员调用compareTo而不是equals。 如果遵循第一点,则在ContactService中,不需要访问联系人列表来验证元素的存在,这是不必要的。 如果您不想在列表中有重复的元素,Java允许您选择另一个集合,该集合是为特定目的而创建的,即TreeSet。 以下是您的ContactService的重新访问版本:


你是说你的测试失败了?你有什么问题?我从来没有使用过jupiter测试类,首先我建议你只使用org.junit类。然后,我们可以一步一步地使用您的代码并使其正常工作。因为assertTruecs.addContactcontact显示为不明确,所以它会出错。如果您能帮助我们了解为什么会发生这种情况,以及如何解决这一问题,我们将不胜感激。

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

}
import static org.junit.Assert.*;
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;
    }
}
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));
    }

}