title: Mockito mock not working on main method java
body: <pre><code>public class example
  {
      public void method()
      {
          System.out.println("Shouldn't be here!");
      }
  }
  </code></pre>
  
  <pre><code>public class examplemain
  {
      public static void main(String[] args)
      {
          example obj = new example();
          obj.method();
          System.out.println("Inside Main");
      }
  }
  </code></pre>
  
  <p>I want to test <em>main</em> only and do not want to call <em>method</em> function.
  I used this-</p>
  
  <pre><code>class examplemainTest
  {
      @Test
      void main()
      {
          example obj = mock(example.class);
          doNothing().when(obj).method();
  
          String[] args = new String[0];
          examplemain.main(args); //line 1
  
          obj.method(); //line 2
      }
  }
  </code></pre>
  
  <p>But still it is calling <em>method</em> function in line 1 and it is only working for line 2.
  Following is the output I got after running the test.</p>
  
  <p>Shouldn't be here!</p>
  
  <p>Inside Main</p>
  
  <p>Process finished with exit code 0</p>