JUnit Test - Class invokes methods in other class

I have a class player which has a method

 feedPet(Pet p1, Food f1){

So the feedPet invokes the pets eat method, the eat method then adjusts the pets hunger level.

To test this I need create a player and food object in my jUnit test case.

I then test that its working with a simple


p1 is the pet object I have created in the player test case.

My question is , is what I am doing appropriate? and am I testing my player class correctly?

Really Its testing if the eat method for the pet works but I also need to check that the feedPet method works for the player as well.

Any thoughts or advice is much appreciated.


What you should do ideally is to create two test files -one for Pet class that tests its eat method and another for player class which tests feedPet Method.

When you write


I hope hunger is the instance variable that was set under eat method of pet class, which as per your expectation is 3. If that's the case you are on right path

Assuming your test looks something like that

public void player_should_feed_a_pet() {
    Food f1 = givenFoodWithHungerReductionOf(1);
    Pet p1 = givenPetWithHunger(4);
    player.feedPet(p1, f1);
    assertEquals(p1.getHunger(), 3);

It is a good test. You're not really testing the Pet.eat() method, but the correct interactions between Player and Pet.

You might also need a separate test for Pet class to cover more cases.

Need Your Help

Method overload ambiguity with Java 8 ternary conditional and unboxed primitives

java java-8 method-overloading

The following is code compiles in Java 7, but not openjdk-

Touch events in Meteor

touch multi-touch meteor

The first thing I started to do with Meteor was start writing a touch-based web application for mobile/tablets. Here's how you reproduce the problem:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.