java - Mockito's spy functionality fails -


i have public class mclass contains public static method getsystime(). in test trying mock 1 particular method of class. need whenever method of class called in test, returns string "faketimestamp". class mclass doesn't have parent class. following code:

mclass m = mockito.spy(new mclass()); mockito.when(m.getsystime()).thenreturn("faketimestamp"); 

however, following error on test:

org.mockito.exceptions.misusing.missingmethodinvocationexception:  when() requires argument has 'a method call on mock'. example:     when(mock.getarticles()).thenreturn(articles);  also, error might show because: 1. stub either of: final/private/equals()/hashcode() methods.    methods *cannot* stubbed/verified. 2. inside when() don't call method on mock on other object. 3. parent of mocked class not public.    limitation of mock engine.       @ com.scb.edmhdpif.filevalidator.cdcrowcountvalidatortest.cdcrowcountfiledrivertest(cdcrowcountvalidatortest.java:77)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:45)     @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15)     @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:42)     @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20)     @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28)     @ org.junit.runners.parentrunner.runleaf(parentrunner.java:263)     @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:68)     @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:47)     @ org.junit.runners.parentrunner$3.run(parentrunner.java:231)     @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:60)     @ org.junit.runners.parentrunner.runchildren(parentrunner.java:229)     @ org.junit.runners.parentrunner.access$000(parentrunner.java:50)     @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:222)     @ org.junit.runners.parentrunner.run(parentrunner.java:300)     @ org.junit.runner.junitcore.run(junitcore.java:157)     @ com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:69)     @ com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:234)     @ com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:74)     @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)     @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)     @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)     @ java.lang.reflect.method.invoke(method.java:606)     @ com.intellij.rt.execution.application.appmain.main(appmain.java:144)   process finished exit code -1 

i using intellij idea 15.0.2. there someway can resolve error or there mock engine can in achieving need?

best regards

i believe problematic part you're trying spy static method. i.e. works on non-static instances:

public class mclass {      public string getsystime() { // tricky 1         return "salala";     } }  public class mtest {      @test     public void testspying() {         mclass m = mockito.spy(new mclass());         mockito.when(m.getsystime()).thenreturn("faketimestamp");     } } 

in this q&a explain techniques on dealing kind of problem.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo