title: Getting NullPointerException performing unit-testing with Mockito
body: <p><br/>
  Please don't mark it as duplicate, I am unable to fix this issue.
  <br/><br/>
  I am writing unit testing using <em>Mockito</em>. While executing the test case, I am getting <code>NullPointerException</code> on the <code>@Autowired bean</code> of <code>MainTemplate class</code>.</p>
  
  <p>Code:</p>
  
  <pre><code>public abstract class Template{
  
    abstract public boolean isEven(String input);
  
    private String getName(String initial){
      return "INIT"+initial;
    }
  
    final public void run(String input){
     isEven(input);
     getName(input);
  
    }
  }
  </code></pre>
  
  <p><br/>
  Implementation of Template:
  <br/></p>
  
  <pre><code>public class MainTemplate extends Template{
  
   @Autowired
   private UserUtil userUtil;
  
      @Override
      public boolean isEven(String input){
        return userUtil.getResult(input);  //GETTING NullPointerException here..
      }
  }
  </code></pre>
  
  <p><br/>
  Bean configuration:</p>
  
  <pre><code>@Configuration
  public class BeanConfiguration{
    @Bean
    public Template getBean(){
       return new MainTemplate();
    }
  }
  
  @Service
  public class Executor{
   @Autowired
   private BeanConfiguration beanConfig;
  
   public void execute(){
     beanConfig.getBean().run("sample input");
   }
  
  }
  </code></pre>
  
  <p>Unit testing class:
  <br/></p>
  
  <pre><code>@SpringBootTest
  @RunWith(MockitoJUnitRunner.class)
  public class MainTemplateTest{
  
    @InjectMocks
    private MainTemplate mainTemplate;
  
    @Mock
    private UserUtil userUtil;
  
    @Test
    public void isEvenTest(){
  
      Mockito.when(userUtil.getResult(anyString())).thenReturn(true);
  
      mainTemplate.isEven("input string");
    }
  }
  </code></pre>
  
  <p><br/>
  I am getting NullPointerException while running the above test.<br/>
  Getting NullPointerException on:
  <br/></p>
  
  <pre><code>    @Override
      public boolean isEven(String input){
        return userUtil.getResult(input);  //GETTING NullPointerException, while calling from mock method
      }
  </code></pre>