mock获取入参数并动态设置返回值
/* | |
* Copyright (c) 2007 Mockito contributors | |
* This program is made available under the terms of the MIT License. | |
*/ | |
package org.mockitousage.stubbing; | |
import org.junit.Test; | |
import org.mockito.Mock; | |
import org.mockito.invocation.InvocationOnMock; | |
import org.mockito.stubbing.Answer; | |
import org.mockitousage.IMethods; | |
import org.mockitoutil.TestBase; | |
import java.lang.reflect.Method; | |
import java.util.Set; | |
import static org.junit.Assert.*; | |
import static org.mockito.Mockito.*; | |
public class StubbingWithCustomAnswerTest extends TestBase { | |
@Mock | |
private IMethods mock; | |
@Test | |
public void shouldAnswer() throws Exception { | |
when(mock.simpleMethod(anyString())).thenAnswer(new Answer<String>() { | |
public String answer(InvocationOnMock invocation) throws Throwable { | |
String arg = invocation.getArgument(0); | |
return invocation.getMethod().getName() + "-" + arg; | |
} | |
}); | |
assertEquals("simpleMethod-test", mock.simpleMethod("test")); | |
} | |
@Test | |
public void shouldAnswerWithThenAnswerAlias() throws Exception { | |
RecordCall recordCall = new RecordCall(); | |
Set<?> mockedSet = (Set<?>) when(mock(Set.class).isEmpty()).then(recordCall).getMock(); | |
mockedSet.isEmpty(); | |
assertTrue(recordCall.isCalled()); | |
} | |
@Test | |
public void shouldAnswerConsecutively() throws Exception { | |
when(mock.simpleMethod()) | |
.thenAnswer(new Answer<String>() { | |
public String answer(InvocationOnMock invocation) throws Throwable { | |
return invocation.getMethod().getName(); | |
} | |
}) | |
.thenReturn("Hello") | |
.thenAnswer(new Answer<String>() { | |
public String answer(InvocationOnMock invocation) throws Throwable { | |
return invocation.getMethod().getName() + "-1"; | |
} | |
}); | |
assertEquals("simpleMethod", mock.simpleMethod()); | |
assertEquals("Hello", mock.simpleMethod()); | |
assertEquals("simpleMethod-1", mock.simpleMethod()); | |
assertEquals("simpleMethod-1", mock.simpleMethod()); | |
} | |
@Test | |
public void shouldAnswerVoidMethod() throws Exception { | |
RecordCall recordCall = new RecordCall(); | |
doAnswer(recordCall).when(mock).voidMethod(); | |
mock.voidMethod(); | |
assertTrue(recordCall.isCalled()); | |
} | |
@Test | |
public void shouldAnswerVoidMethodConsecutively() throws Exception { | |
RecordCall call1 = new RecordCall(); | |
RecordCall call2 = new RecordCall(); | |
doAnswer(call1) | |
.doThrow(new UnsupportedOperationException()) | |
.doAnswer(call2) | |
.when(mock).voidMethod(); | |
mock.voidMethod(); | |
assertTrue(call1.isCalled()); | |
assertFalse(call2.isCalled()); | |
try { | |
mock.voidMethod(); | |
fail(); | |
} catch (UnsupportedOperationException e) { | |
} | |
mock.voidMethod(); | |
assertTrue(call2.isCalled()); | |
} | |
@Test | |
public void shouldMakeSureTheInterfaceDoesNotChange() throws Exception { | |
when(mock.simpleMethod(anyString())).thenAnswer(new Answer<String>() { | |
public String answer(InvocationOnMock invocation) throws Throwable { | |
assertTrue(invocation.getArguments().getClass().isArray()); | |
assertEquals(Method.class, invocation.getMethod().getClass()); | |
return "assertions passed"; | |
} | |
}); | |
assertEquals("assertions passed", mock.simpleMethod("test")); | |
} | |
private static class RecordCall implements Answer<Object> { | |
private boolean called = false; | |
public boolean isCalled() { | |
return called; | |
} | |
public Object answer(InvocationOnMock invocation) throws Throwable { | |
called = true; | |
return null; | |
} | |
} | |
} |
2. 当mock一个对象,且执行此对象中的方法没有返回值时,使用下面的方法:
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
类名 对象 = Mockito.mock(类名.class);
Mockito.doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
return "called with arguments: " + args;
}
}).when(对象).方法名();
--------------------- 本文来自 flysun3344 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/flysun3344/article/details/52065492?utm_source=copy
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)