0

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?

2
  • Help you how? You have the findPet method, what did you want to test? Whether it returns the right pos? Commented Dec 14, 2020 at 15:18
  • I want to test that the method actually finds Pet, i know I could just write code so i know it works but Im doing a couple of exercises on JUnit and want to know how to test it in JUnit Commented Dec 14, 2020 at 15:21

2 Answers 2

1

Your test is trying to remove a pet but presumably should be exercising the findPet method. Assuming that's the case something like this using JUnit assertions:

@Test
public void testFindPet() {
    // Create an owner and some pets
    Owner owner = new Owner("OID57","John"); 
    owner.addPet("PID01","dog","bowwow",4); 
    owner.addPet("PID02","dog","shep",6); 
    owner.addPet("PID03","cat","meow",4); 
    owner.addPet("PID04","snake","wally",2);

    // Find a pet for this owner
    assertEquals(0, owner.findPet("P1D01"));

    // Check unknown pet
    assertEquals(-1, owner.findPet("unknown"));
}

(Also assumes the findPet method is a member of the owner class).

You might also want to look into using @BeforeEach to create an owner and pets that can be used across multiple test methods.

Sign up to request clarification or add additional context in comments.

Comments

0

I suggest you this

Owner owner;

@Before
public void setup() {
   owner = new Owner();
   //add pets to owner so that you dont have to do this in every test
   owner.addPet("PID01","dog","bowwow",4); 
   owner.addPet("PID02","dog","shep",6); 
   owner.addPet("PID03","cat","meow",4); 
   owner.addPet("PID04","snake","wally",2);
}

@Test
public void testFindPet() {
   int result = owner.findPet("PID04");
   owner.removePet("PID04");
   //checks if it removed certain pet
   assertNotEquals(result, owner.findPet("PID04"));
}

1 Comment

I think you are testing both findPet and removePet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.