带有最终参数的模拟方法

Mocking method with final arguments

我有一些方法在 ServiceClassA 中使用最终字符串参数。

@Service
public class ServiceClassA {

    public String callingMethod(final String argOne, final String argTwo) {
        //Implementation here
    }
    
}

它在另一个 class ServiceClassB 的另一个方法中调用。

@Service
public class ServiceClassB {
    private final ServiceClassA serviceClassA;
    
    public MyResponse methodB() {
        String inputOne = "111";
        String inputTwo = MyEnum.AAA.getDescription();
        
        final String result = serviceClassA.callingMethod(inputOne,inputTwo);
        
        //some implementations
        
        MyResponse response = new MyResponse();
        //set values to response
        
        return response;
    }
}

还有一个枚举。

public enum MyEnum {
    AAA(1, "AAA"), BBB(2,"BBB");
    
    private static final Map<Integer, MyEnum> MYENUM_MAP = new HashMap<>();
    
    static {
        for (MyEnum myEnum : values()) {
            MYENUM_MAP.put(myEnum.getValue(), myEnum);
        }
    }
    
    private final int value;
    private final String description;

    private MyEnum(int value, String description) {
        this.value = value;
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public int getValue() {
        return value;
    }
}

我正在使用 mockito 和 JUnit 测试 ServiceClassB。当我尝试在 ServiceClassA 中模拟 callingMethod 时,它 returns null 而不是“SUCCESS”。

@RunWith(MockitoJUnitRunner.class)
public class ServiceClassBTest {
    
    @InjectMocks
    private ServiceClassB serviceClassB;

    @Mock
    private ServiceClassA serviceClassA;
    
    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
        
    @Test
    public void methodB_success(){
        String result = "SUCCESS";
        
        when(serviceClassA.callingMethod(Mockito.anyString(), Mockito.anyString())).thenReturn(result); 
    }
}

我尝试了以下几种方法,但总是 returns 无效。但我想获得成功。

when(myService.callingMethod(Mockito.anyString(), Mockito.anyString())).thenReturn(result); //return null
when(myService.callingMethod(Mockito.any(), Mockito.any())).thenReturn(result); //return null
when(myService.callingMethod(ArgumentMatchers.anyString(), ArgumentMatchers.anyString())).thenReturn(result); //return null
doReturn(result).when(myService).callingMethod(Mockito.anyString(), Mockito.anyString()); //return null

问题在于您正在使用 @RunWith(MockitoJUnitRunner.class) together with MockitoAnnotations.initMocks(this)。他们是矛盾的。
使用一个 MockitoJUnitRunnerMockitoAnnotations.initMocks

MockitoJUnitRunner Initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method.

工作测试:

@RunWith(MockitoJUnitRunner.class)
public class ServiceClassBTest {
    
    @InjectMocks
    private ServiceClassB serviceClassB;

    @Mock
    private ServiceClassA serviceClassA;
        
    @Test
    public void methodB_success(){
        String result = "SUCCESS";
        
        when(serviceClassA.callingMethod(Mockito.anyString(), Mockito.anyString())).thenReturn(result);

        serviceClassB.methodB();
    }
}