<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	>
<channel>
	<title>
	Comments on: 3 basic mistakes for NullPointerException when Mock	</title>
	<atom:link href="https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 22 May 2021 17:02:42 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>
		By: Andrej Buday		</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-2470</link>

		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Tue, 09 Feb 2021 20:30:39 +0000</pubDate>
		<guid isPermaLink="false">http://codepills.com/?p=958#comment-2470</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-2467&quot;&gt;ajith&lt;/a&gt;.

Hi Ajith

From first glance, I think your problem is with the Spring application context. You are running a Mock test with @RunWith(MockitoJunitRunner.class). However, maybe you want to @RunWith(SpringRunner.class) as the SpringRunner provides support for loading a Spring Application Context and having beans @Autowired into your test instance. MockitoJunitRunner will you create a Mock object based on the type you demand - StockService in your guess. Have you tried to go this way? Hope it helped.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-2467">ajith</a>.</p>
<p>Hi Ajith</p>
<p>From first glance, I think your problem is with the Spring application context. You are running a Mock test with @RunWith(MockitoJunitRunner.class). However, maybe you want to @RunWith(SpringRunner.class) as the SpringRunner provides support for loading a Spring Application Context and having beans @Autowired into your test instance. MockitoJunitRunner will you create a Mock object based on the type you demand &#8211; StockService in your guess. Have you tried to go this way? Hope it helped.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: ajith		</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-2467</link>

		<dc:creator><![CDATA[ajith]]></dc:creator>
		<pubDate>Tue, 09 Feb 2021 13:28:20 +0000</pubDate>
		<guid isPermaLink="false">http://codepills.com/?p=958#comment-2467</guid>

					<description><![CDATA[my code

public class StockController{
  @Autowired
  private StockService stockService;

  public GatewayResponse  findProduct(String productId) {
try{
    Optional optional = stockService.getProduct(productId);
    if (optional.isPresent()) {
      Product product = optional.get();
      return new GatewayResponse(HttpStatus.Ok,product, Message.SUCCESS.getDesc());
    } 
   return new GatewayResponse(HttpStatus.NO_CONTENT,product, Message.SUCCESS.getDesc());
}
catch(Exception e)
{ 
log.info(&quot;bad product id........&quot;);
}
  }

******************
Test class

@RunWith(MockitoJunitRunner.class)
public void ControllerTest()
{
@InjectMocks
StockController stockController;
@Mock
StockService stockService;
@Test
public void findProductTest()
{
 String productId = &quot;pr455&quot;;
Product pr = new Product();
pr.setName(&quot;buhbdf&quot;);
   when(stockService.getProduct(productId)).thenReturn(Optional.of(product));
  MvcResults mvcResults = mockMvc.perform(get(&quot;/product&quot;).param(&quot;id&quot;, productId)).andExpect(Status().isOk()).andReturn();
}

NOTE: just exclude if any syntax exceptions it might be my typing mistakes. every thing is fine just getting NullpointerException. i declared MockMvc object also bt didn&#039;t mension here

when debugging in StockController i am getting null pointer Exception in ----&#062;   if (optional.isPresent())]]></description>
			<content:encoded><![CDATA[<p>my code</p>
<p>public class StockController{<br />
  @Autowired<br />
  private StockService stockService;</p>
<p>  public GatewayResponse  findProduct(String productId) {<br />
try{<br />
    Optional optional = stockService.getProduct(productId);<br />
    if (optional.isPresent()) {<br />
      Product product = optional.get();<br />
      return new GatewayResponse(HttpStatus.Ok,product, Message.SUCCESS.getDesc());<br />
    }<br />
   return new GatewayResponse(HttpStatus.NO_CONTENT,product, Message.SUCCESS.getDesc());<br />
}<br />
catch(Exception e)<br />
{<br />
log.info(&#8220;bad product id&#8230;&#8230;..&#8221;);<br />
}<br />
  }</p>
<p>******************<br />
Test class</p>
<p>@RunWith(MockitoJunitRunner.class)<br />
public void ControllerTest()<br />
{<br />
@InjectMocks<br />
StockController stockController;<br />
@Mock<br />
StockService stockService;<br />
@Test<br />
public void findProductTest()<br />
{<br />
 String productId = &#8220;pr455&#8221;;<br />
Product pr = new Product();<br />
pr.setName(&#8220;buhbdf&#8221;);<br />
   when(stockService.getProduct(productId)).thenReturn(Optional.of(product));<br />
  MvcResults mvcResults = mockMvc.perform(get(&#8220;/product&#8221;).param(&#8220;id&#8221;, productId)).andExpect(Status().isOk()).andReturn();<br />
}</p>
<p>NOTE: just exclude if any syntax exceptions it might be my typing mistakes. every thing is fine just getting NullpointerException. i declared MockMvc object also bt didn&#8217;t mension here</p>
<p>when debugging in StockController i am getting null pointer Exception in &#8212;-&gt;   if (optional.isPresent())</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: ajitha		</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-2465</link>

		<dc:creator><![CDATA[ajitha]]></dc:creator>
		<pubDate>Tue, 09 Feb 2021 13:03:44 +0000</pubDate>
		<guid isPermaLink="false">http://codepills.com/?p=958#comment-2465</guid>

					<description><![CDATA[Followed ur steps well bt still facing the null pointer exceptions .
in testing class it mocked object is mocking perfectly but when it goes to corresponding class that mocked reference is becoming null...

plss help me to solve this]]></description>
			<content:encoded><![CDATA[<p>Followed ur steps well bt still facing the null pointer exceptions .<br />
in testing class it mocked object is mocking perfectly but when it goes to corresponding class that mocked reference is becoming null&#8230;</p>
<p>plss help me to solve this</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Null pointer exception when stubbing &#8211; Ask Android Questions		</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-1735</link>

		<dc:creator><![CDATA[Null pointer exception when stubbing &#8211; Ask Android Questions]]></dc:creator>
		<pubDate>Mon, 02 Nov 2020 11:43:46 +0000</pubDate>
		<guid isPermaLink="false">http://codepills.com/?p=958#comment-1735</guid>

					<description><![CDATA[[&#8230;] to this blog post : https://codepills.com/2018/05/10/3-basic-mistakes-for-nullpointerexception-when-mock/ I should replace when thenreturn by when then (answer) which if it&#8217;s true, why [&#8230;]]]></description>
			<content:encoded><![CDATA[<p>[&#8230;] to this blog post : <a href="https://codepills.com/2018/05/10/3-basic-mistakes-for-nullpointerexception-when-mock/" rel="ugc">https://codepills.com/2018/05/10/3-basic-mistakes-for-nullpointerexception-when-mock/</a> I should replace when thenreturn by when then (answer) which if it&#8217;s true, why [&#8230;]</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jagadeesh Uppalapati		</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comment-1448</link>

		<dc:creator><![CDATA[Jagadeesh Uppalapati]]></dc:creator>
		<pubDate>Thu, 17 Sep 2020 11:22:31 +0000</pubDate>
		<guid isPermaLink="false">http://codepills.com/?p=958#comment-1448</guid>

					<description><![CDATA[I fallowed above rules correctly.
while doing Mock to Window geting null instance
@Mock
Window window;]]></description>
			<content:encoded><![CDATA[<p>I fallowed above rules correctly.<br />
while doing Mock to Window geting null instance<br />
@Mock<br />
Window window;</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
