0

With a given enum like

public enum Colors {
  RED("ff0000"),
  BLUE("0000ff"),
  SILVER("eeeeee");

  private String code;

  public Colors(String code) {
    this.code = code;
  }

  public void paint(Car car) {
    car.applyColor(code);
  }
}

I'd like to verify in a test, that paint is called with a certain car on enum RED. I thought about mocking the enum class (mockStatic) or spying on it, but couldn't find a way yet to verify the method call. I want to avoid calling the real method implementation on the test.

Please note: This is a very simplified example of the real code

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • did you check answer in https://stackoverflow.com/questions/21202576/how-to-mock-a-method-in-an-enum-class ? or https://stackoverflow.com/questions/38224517/mock-an-enum-using-mockito-in-java – Ori Marko Jun 14 '22 at 08:34
  • @user7294900 - yes, both. One provides a solution for static methods. That's not a big challenge with `MockedStatic`. The other question was about mocking the full enum. Here, I look for a solution to verify that a (non-static) method is called on the expected enum. – Andreas Dolk Jun 14 '22 at 11:14

0 Answers0