Comments (3)
java.lang.UnsupportedOperationException: elapsedRealtime is native
at android.os.SystemClock.elapsedRealtime(SystemClock.java)
from androidbox.
使用的框架
- powermock
- junit
from androidbox.
Mock static 方法
先mock这个类
@PrepareForTest({ ScreenUtil.class, NetUtil.class})
PowerMockito.mockStatic(NetUtil.class);
其他代码
@Test
public void test0() throws Exception {
MockHeartAction action = new MockHeartAction();
Context context = PowerMockito.mock(Context.class);
//PowerMockito.doReturn("networkTag").when(NetUtil.getNetworkTag(context));//代码[1.2]
// PowerMockito.when(NetUtil.getNetworkTag(context)).thenReturn("networkTag");//代码[1.1]
PowerMockito.when(NetUtil.getNetworkTag(Mockito.any(Context.class))).thenReturn("networkTag");//代码[1.3]
String string = NetUtil.getNetworkTag(context);
LogUtil.d("==="+string);
ClassA a = new ClassA();
a.test();
}
class A{
public void a{
String networkTag = NetUtil.getNetworkTag(heartContext.getContext());
LogUtil.d(TAG, "===XXX" + networkTag);
}
}
错误一:
// 执行代码[1.2]
PowerMockito.doReturn("networkTag").when(NetUtil.getNetworkTag(context));//代码[1.2]
//报的异常
org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at com.xtc.im.core.push.heartbeat.state.ActiveStateTest.heartbeat0(ActiveStateTest.java:59)
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, you naughty developer!
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed
at org.powermock.core.MockGateway.doMethodCall(MockGateway.java:124)
at org.powermock.core.MockGateway.methodCall(MockGateway.java:63)
at com.xtc.im.core.common.utils.NetUtil.getNetworkTag(NetUtil.java)
- 可见这个doReturn 在前,when 在后,不可行,因为when在后是when(obj).get(),以一个obeject进行调用的方式。去mock 这个when 里面的obj对象的方法。
错误二:
执行代码[1.1]中发现还有个问题,返回的值不对
发现打印:
- ===networkTag
- ===XXXnull
最后改为执行代码[1.3]
打印结果:
- ===networkTag
- ===XXXnetworkTag
原因分析:
NetUtil.getNetworkTag(Mockito.any(Context.class)),里面的传入的Context要是一个Context的class类型,那么到了其他地方也就会识别到这种类型,然后按照return正确的返回。
扩展知识(待完善)
- when(mock某个方法) thenreturn(“定义的返回值”);
- doReturn(“定义的返回值”)when(“对象”).get();
源码解读
待续
from androidbox.
Related Issues (20)
- Toast Show的时候出现 BadTokenException: Unable to add window -- token
- java.lang.IllegalStateException: Fragment already added HOT 2
- ANR: "main" prio=5 tid=1 WaitingForGcToComplete HOT 2
- Exception: The specified message queue synchronization barrier token has not been posted or has already been removed.
- ExceptionInInitializerError HOT 2
- 为什么Receiver不允许进行BindService
- Fragment生命周期
- SQLite多线程问题 HOT 3
- 《Android应用性能优化》 HOT 1
- 内存优化 HOT 1
- Native闪退 HOT 1
- java.lang.IllegalStateException: The specified child already has a parent.
- RxJava线程 HOT 1
- Service启动成功onCreate和onStartCmmand都不回调
- CursorWindowAllocationException HOT 1
- SharedPreferences解析
- Component class com.huawei.hms.support.api.push.PushEventReceiver does not exist in com.xtc.watch HOT 2
- SQLite查询操作源码分析:从 IllegalArgumentException:the bind value at index 2 is null 说起
- startActivityForResult的坑
- java.lang.IllegalArgumentException: pointerIndex out of range HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from androidbox.