java - How can I mock a local final variable -


there local variable in method final. how can mock?

public void method(){   final int i=myservice.getnumber(); } 

i want mock

when(myservice.getnumber()).thenreturn(1); 

how can complete mocking?

my project using java 7, there way using reflection or else realize mock

this request, stated, doesn't make sense. mocking systems don't mock variables (or fields). could, however, set field object have mocked. test this:

@test public void methodwithnumberone {   myservice myservice = mockito.mock(myservice.class);   when(myservice.getnumber()).thenreturn(1);    // might want set myservice constructor argument, instead.   systemundertest systemundertest = new systemundertest();   systemundertest.myservice = myservice;    systemundertest.method(); } 

another way set doesn't require mocks:

public void method() {   method(myservice.getnumber()); }  /** use testing, set arbitrary number. */ void method(final int i) {   // ... } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

wso2esb - How to concatenate JSON array values in WSO2 ESB? -