Java – specifies the specific enumeration passed to the method in the mockito doreturn method

I have a JUnit test. I have an object simulated in a class Let's call the mocker class with @ mock of mymanager named mocker

Example class:

public class Mocker {
   private MyManager myManager;

   public void myMethod() {
       String x = "test";
       final String result1 =  this.myManager.execute(dummyEnum.ENUM_A,x);
       final String result2 =  this.myManager.execute(dummyEnum.ENUM_B,x);

       if(result1 == true) {
           //Do something
       }
       if(result2 == true) {
           //Do something else
       }
   }

   public enum dummyEnum {
        ENUM_A,ENUM_B
   }
}

My current JUnit test uses the following: do return (null) when(mocker). execute(any(dummyEnum.class),anyObject());

However, for result1 and amp; Will return null Result 2 How to specify when using enum_ When a executes (), it returns a string of hello and enum_ B's execute() returns a Goodyear string

I've seen answer here, but I don't want to just say any instances of this class. I want to specify an enumeration from this class

Solution

Use the EQ () method of the matchers class (for equals)

Mockito.doReturn("Hello").when(mock).execute(Matchers.eq(dummyEnum.ENUM_A),anyObject());

Mockito.doReturn("Goodbye").when(mock).execute(Matchers.eq(dummyEnum.ENUM_B),anyObject());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>