Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Source Link
Ani
  • 29
  • 1
  • 1
  • 3

Mockito: mocking member variables

I have the following code:

public class ABC { private double a; Other_object other_object; public ABC(int a, int[] b){ this.a=a; other_object=new Other_object(b); } ... public int method_1(){ return other_object.other_method(); } public int method_2(){ if(method_1()>0 && a>0) return 1; else return 0; } 

}

I've created the following mock object: ABC a=mock(ABC.class); Mockito.when(a.method1()).thenReturn(0);

I cannot set a value for "int a". I tried to create a setter function in ABC, and mocking it, but it doesn't work. I would like to know why mocking the setter function doesn't work how can I mock this value (int a)?