在不使用类对象的情况下调用另一个方法中的方法

时间:2021-06-23 14:23:01

标签: java junit mocking mockito

我正在尝试模拟从另一个方法调用的方法,而不使用类对象。在指定的代码中,我想在不使用对象的情况下模拟调用 methodB() 的 methodA()。

我无法更改现有课程中的任何内容。

class A{

public String methodA(){
//do something
String s = methodB(employee);
}

public String methodB(Employee e){
 e.getId();
}
}

我已经尝试过:

  1. Mockito.doReturn("id").when(objectOfA).methodB(employee);
  2. when(methodB(employee)).thenReturn("id");

1 个答案:

答案 0 :(得分:0)

A a = mock(A.class);
when(a.methodA()).thenReturn("Whatever you want");