惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
博客园_首页
罗磊的独立博客
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
Last Week in AI
Last Week in AI
D
DataBreaches.Net
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
D
Docker
MongoDB | Blog
MongoDB | Blog
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
H
Help Net Security
T
The Blog of Author Tim Ferriss

Yusuf Aytas

When Code Is Cheap, Does Quality Still Matter? Why Crouching Tiger, Hidden Dragon Is a Masterpiece Why We Ignore Advice The Mirror Is Part of the Machine When Too Many Maps Overlap on One Person The Work Runs on Different Maps Your Work Introduces You Trial By Fire The Dude Why Headcount Math Lies Capacity Is the Roadmap The Roadmap Is Not the System Torres del Paine W Trek Escaping Status Theater Incentives Drive Everything Scaling Culture Without Dilution What Good Looks Like Why Airport Security Feels Random Why Politics Appear How to Work with Me The Janus Protocol Multi-Horizon Delivery Framework What Good Execution Looks Like Managing Your Manager Why Kingdom of Heaven’s Director’s Cut Is Better AI Broke Interviews Most of What We Call Progress Managers Have Been Vibe Coding All Along Stop Wasting Brainpower Why Over-Engineering Happens
Mock methods that manipulate parameters
Yusuf Aytas · 2015-08-24 · via Yusuf Aytas

Published · 2 min read

Recently, I've worked a little bit on redis cache for Imcache. I needed to mock a behavior of a method where the method manipulates given parameters e.g. change state or call another method. After a little bit of research, I've found out there's a nice mockito feature that accomplishes what I want: doAnswer. Basically, doAnswer allows stubbing a void method with a generic answer. While answering the method call, you can do whatever you wish. So, here's an example usage for void method

doAnswer(new Answer() {
  public Object answer(InvocationOnMock invocation) {
    //Get the arguments and manipulate them as you wish.
    Object\[\] args = invocation.getArguments();
    Mock mock = invocation.getMock();
    //This is a void method so we return null.
    return null;
 }
}).when(mock).someMethod();

Another example for a non-void method.

doAnswer(new Answer() {
  public Object answer(InvocationOnMock invocation) {
    Object\[\] args = invocation.getArguments();
    byte\[\] actualBytes = ((byte\[\]) args\[0\]);
    for (int i = offset; i < offset + length; i++) {
      actualBytes\[i - offset\] = bytes\[i\];
    }
    return length;
  }
}).when(mock).someMethod();

You can check out how I've made use of doAnswer for real at RedisStreamReaderTest.

doAnswer might come in handy whenever you need to call another method inside the void method. Method you're calling might be a dependency for your project so you may not have ability change method signature.