lundi 14 décembre 2020

JUnit Testing if an object is in an arraylist

Hey guys just started using JUnit I have a pet and owner class and in the owner class I am currently trying to write a test for for finding a certain Pet, and to use a remove methode remove a Pet and test to see that that pet has gone. I have an method in owner called find pet which takes in an id cycles through all the pets in the ArrayList and prints out the pet with the matching id

public int findPet(String petId)
    {
        int pos = -1;
        int i = 0;
        while (i < pets.size() && pos == -1)
        {
            if (pets.get(i).getPetId().equalsIgnoreCase(petId))
            {
                pos = i;
            }
            else
            {
                i++;
            }
        }
        return pos;
    }

Heres what I have so far:

@Test
    public void testFindPet() {
        
        System.out.println("Testing the FindPet() method");
        Owner o1 = new Owner("OID57","John"); 
        o1.addPet("PID01","dog","bowwow",4); 
        o1.addPet("PID02","dog","shep",6); 
        o1.addPet("PID03","cat","meow",4); 
        o1.addPet("PID04","snake","wally",2);
        
        o1.removePet("PID01");
   
    }

So would anyone be able to help me find out how to write a test for finding a pet?

Aucun commentaire:

Enregistrer un commentaire