0

Hypothetically I have

class Box{
   Item[] items;
}

class Item{
   Toy[] toys;
}

class Toy{
   Color[] colors;
}

The method is to test if the Box has green color toy;

public bool testHasColor(Box){
   //There will be code here that
   //Streams over each type till we get to color
   // Final point is
   if(color==Color.GREEN){
    return true;
   }
}

BoxText.class

public void testBoxHasColorGreenMethod(){
    // Need mocking here
}

What the best way to mock this Box class for the test case?

Thank you

2
  • 1
    I don't really get what you're trying to mock here. Is it correct that you're trying to test testHasColor method? If so, why would you mock the Box class, cause it seems to me you'd like to just pass some objects of this class to the testHasColor method and check whether the result is expected or not. Commented Jan 31, 2019 at 13:16
  • Well wihtout knowing anything more of your code, in Java i think you shouldn't use arrays, but some subclass of Collection. But anyways could you give us a bit more detail (also please red this: stackoverflow.com/help/mcve ) Commented Jan 31, 2019 at 13:20

1 Answer 1

1

You don't need to mock any Java APIs. You just need to "mock" some data which is usually not called "mocking" - just testing. So you would just create a test instance of Box and test if your method produces the expected outcome.

    Box box = new Box();
    Item item = new Item();
    Toy toy = new Toy();

    box.items = new Item[] {item};
    item.toys = new Toy[] {toy};
    toy.colors = new Color[] {Color.RED, Color.GREEN};
Sign up to request clarification or add additional context in comments.

Comments

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.