title: Not able to use @Rule & @RunWith annotation, but the deprecated initmocks() works
body: <p>I am working on a basic test-driven development learning of a simple java program which provides portfolio value for the stocks.</p>
  <p>I am having 2 classes,<code>Portfolio.java</code> &amp; <code>Stock.java</code> which depicts the portfolio &amp; stock models. An interface <code>StockService.java</code> is used in abstract manner to get real-time stock price.</p>
  <p><code>PortfolioTest.java</code> is the class in which I am trying to write unit-tests for the features of portfolio by mocking this StockService using Mockito.</p>
  <p>I am able to use the deprecated <code>MockitoAnnotations.initMocks(this);</code> &amp; run my tests but getting null pointer exception if try to use <code>@Rule</code> or <code>@RunWith</code> annotations.</p>
  <p><code>Stock.java</code></p>
  <pre class="lang-java prettyprint-override"><code>public class Stock {
      private String name;
      private int quantity;
  
      public Stock(String name, int quantity) {
          this.name = name;
          this.quantity = quantity;
      }
  
      public String getName() { return name; }
  
      public float getQuantity() { return quantity; }
  }
  </code></pre>
  <p><code>Portfolio.java</code></p>
  <pre class="lang-java prettyprint-override"><code>import java.util.List;
  
  public class Portfolio {
      private List&lt;Stock&gt; stocks;
      private StockService stockService;
      private Float portfolioValue;
  
      public Portfolio(List&lt;Stock&gt; stocks, Float portfolioValue) {
          this.stocks = stocks;
          this.portfolioValue = portfolioValue;
      }
  
      public void setStockService(StockService stockService) { this.stockService = stockService; }
  
      public Float calculateMarketValue() {
          Float marketValue = 0.0f;
          for(Stock stock: this.stocks) {
              marketValue += (stock.getQuantity()*stockService.getRealtimePrice(stock.getName()));
          }
          return marketValue;
      }
  
      public Boolean isInProfit() {
          return (portfolioValue&lt;calculateMarketValue()?true:false);
      }
  }
  </code></pre>
  <p><code>StockService.java</code></p>
  <pre class="lang-java prettyprint-override"><code>public interface StockService {
      public float getRealtimePrice(String name);
  }
  </code></pre>
  <p><code>pom.xml</code></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;project&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
              &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
              &lt;version&gt;5.5.2&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
              &lt;artifactId&gt;junit-platform-runner&lt;/artifactId&gt;
              &lt;version&gt;1.5.2&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.mockito&lt;/groupId&gt;
              &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
              &lt;version&gt;3.5.13&lt;/version&gt;
          &lt;/dependency&gt;
  
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.8.1&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;source&gt;${maven.compiler.source}&lt;/source&gt;
                      &lt;target&gt;${maven.compiler.target}&lt;/target&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.22.0&lt;/version&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                  &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                  &lt;version&gt;3.2.0&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;finalName&gt;mockito-basic&lt;/finalName&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  <p><code>PortfolioTest.java</code></p>
  <pre class="lang-java prettyprint-override"><code>//@RunWith(MockitoJUnitRunner.class)
  public class PortfolioTestMockAnnotations {
  
      //@Rule public MockitoRule rule = MockitoJUnit.rule();
  
      @Mock
      StockService stockService;
  
      @InjectMocks
      Portfolio portfolio;
  
      @BeforeAll
      static void setUp() {
  
      }
  
      @BeforeEach
      void init(){
          MockitoAnnotations.initMocks(this);
          System.out.println(stockService);
          when(stockService.getRealtimePrice(&quot;infosys&quot;)).thenReturn(2200.0f);
          when(stockService.getRealtimePrice(&quot;reliance&quot;)).thenReturn(3100.0f);
          when(stockService.getRealtimePrice(&quot;indiamart&quot;)).thenReturn(4000.0f);
  
          List&lt;Stock&gt; stocks = new ArrayList&lt;&gt;();
          stocks.add(new Stock(&quot;infosys&quot;,10));
          stocks.add(new Stock(&quot;reliance&quot;, 5));
          portfolio = new Portfolio(stocks, 35000.0f);
          portfolio.setStockService(stockService);
      }
  
      @Test
      public void calculateMarketValueTest() {
          Assertions.assertEquals(portfolio.calculateMarketValue(),37500);
      }
  
      @Test
      public void calculateIsInProfitTest() {
          Assertions.assertTrue(portfolio.isInProfit());
      }
  }
  </code></pre>
  <hr />
  <p><strong>Using initmocks()</strong> in the PortfolioTest.java runs the test smoothly.
  <a href="https://i.stack.imgur.com/Vyf9P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vyf9P.png" alt="enter image description here" /></a></p>
  <hr />
  <p><strong>Using @Rule</strong>, throws NPE
  <a href="https://i.stack.imgur.com/N89In.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/N89In.png" alt="enter image description here" /></a></p>
  <hr />
  <p><strong>Using @RunWith</strong>, throws NPE
  <a href="https://i.stack.imgur.com/UpsFk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UpsFk.png" alt="enter image description here" /></a></p>
  <hr />
  <p>Please suggest the correct way for using @Rule &amp; @RunWith in a smooth. Also provide brief difference between these 3 mechanisms of instantiating mocks.</p>