title: Mockito: how to verify method call of a singelton class from another singelton class
body: <p>I am new to Unit testing and Mockito, how to write test code for given code snippet which is singleton.</p>
  
  <p><strong>1. I want to write unit test for class XYZ.</strong><br>
  <strong>2. I want to verify the whether methods get called or not.</strong><br>
  <strong>3. I want to write test code to check state of temp variable.</strong><br>
  <strong>4. I want to write test code for private method calculator also.</strong></p>
  
  <p>class ABC is singleton and has two fields num and square.</p>
  
  <pre><code>class ABC{
        private static ABC instance;
        private int num;
        private int square;
  
        private ABC() {}
  
        public static ABC getInstance() {
            if (instance == null) {
                instance = new ABC();
            }
            return instance;
        }
  
        public int getSquar() {
            return square;
        }
  
        public void setSquar(int square) {
            this.square = square;
        }
  
        public int getNum() {
            return num;
        }
  
        public void setNum(int num) {
            this.num = num;
        }
  }
  </code></pre>
  
  <p>class XYZ which is singleton is calling getter and setter from class ABC is also singleton. </p>
  
  <pre><code>class XYZ{
       private static XYZ instance;
       private int result;
  
       private XYZ(){}
  
       public static XYZ getInstance(){
            if (instance == null) {
                instance = new XYZ();
            }
            return instance;
       }
  
       public void calculateSquare(){
           ABC.getInstance().setNum(5);
           int n = ABC.getInstance().getNum();
           result = calculator(n);
           ABC.getInstance().setSquare(result);
  
       }  
  
       private int calculator(int n){
           return n*n;
       }
  }
  </code></pre>
  
  <p>here i am calling calculateSquare method of class XYZ.</p>
  
  <pre><code>XYZ.getInstance().calculateSquare();
  </code></pre>