Giter VIP home page Giter VIP logo

Comments (3)

zengjingfang avatar zengjingfang commented on September 21, 2024
java.lang.UnsupportedOperationException: elapsedRealtime is native
	at android.os.SystemClock.elapsedRealtime(SystemClock.java)

from androidbox.

zengjingfang avatar zengjingfang commented on September 21, 2024

使用的框架

  • powermock
  • junit

from androidbox.

zengjingfang avatar zengjingfang commented on September 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.