最终参数的模拟方法
我有一些在ServiceClassa中使用最终字符串参数的方法。
@Service
public class ServiceClassA {
public String callingMethod(final String argOne, final String argTwo) {
//Implementation here
}
}
并在其他类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中嘲笑呼叫的方法时,它会返回null而不是“成功”。
@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);
}
}
我尝试了下面的几种方式,并且总是返回零。但是我想取得成功。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您正在使用@runwith(mockitojunitrunner.class)与 mockitoannotations.initmocks(this)。他们矛盾。
使用一个
MockitoJunitRunner
或mockitoAnnotations.initmocks
。工作测试:
The problem there is that you are using @RunWith(MockitoJUnitRunner.class) together with MockitoAnnotations.initMocks(this). They are conflicting.
Use something one
MockitoJUnitRunner
orMockitoAnnotations.initMocks
.Working test: