简体   繁体   中英

Mocking method with final arguments

I have some method which use final String arguments in ServiceClassA.

@Service
public class ServiceClassA {

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

And its call within another method in another 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;
    }
}

Also there is a enum.

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;
    }
}

I'm testing ServiceClassB using mockito and JUnit. When I try to mock callingMethod in ServiceClassA it returns null instead of the "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); 
    }
}

I tried several ways as below and always it returns null. But I want to get SUCCESS as the result.

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

The problem there is that you are using @RunWith(MockitoJUnitRunner.class) together with MockitoAnnotations.initMocks(this) . They are conflicting.
Use something one MockitoJUnitRunner or MockitoAnnotations.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.

Working test:

@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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM