<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mock &#8211; CodePills.com</title>
	<atom:link href="https://codepills.com/tag/mock/feed/" rel="self" type="application/rss+xml" />
	<link>https://codepills.com</link>
	<description>Helping you make a better code</description>
	<lastBuildDate>Sat, 22 May 2021 17:02:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<item>
		<title>3 basic mistakes for NullPointerException when Mock</title>
		<link>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/</link>
					<comments>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/#comments</comments>
		
		<dc:creator><![CDATA[Andrej Buday]]></dc:creator>
		<pubDate>Thu, 10 May 2018 12:00:47 +0000</pubDate>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[mock]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[mockito]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://codepills.com/?p=958</guid>

					<description><![CDATA[This article is a shortlist of the three most common reasons why you get NullPointerException in your tests. <a href="https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
										<content:encoded><![CDATA[<p>You might get <code>NullPointerException</code> exception when you try to mock object in your tests. This article is a shortlist of the three most common reasons why this might be happening.</p>
<p><span id="more-958"></span></p>
<p>Here is a working example of DocumentRepositoryTest class for reference:</p>
<pre><code class="language-java">@RunWith(MockitoJUnitRunner.class)
public final class DocumentRepositoryTest {

    @Mock
    private DocumentRepository documentRepository;

    @Test
    public void testConvertToNewDocument() {
        String paperColor = new String("white");
        Mockito.when(this.documentRepository.getPaperColor()).thenReturn(paperColor);
        String color = this.documentRepository.getDocumentColor();
        Assert.assertThat(color, is(new String("white"));
    }
}</code></pre>
<p>DocumentRepositoryTest class is mocking <code>documentRepository</code> object.</p>
<p>So you are running your test and suddenly you see <code>NullPointerException</code>:</p>
<pre><code class="language-bash">java.lang.NullPointerException at com.your.custom.clazz.Method.insertTest(CustomServiceTest.java:50)</code></pre>
<p>You looked it up, and it seems your test is written correctly. So what might be a problem if you know how the mocking works?</p>
<p>If you are sure by your mocking skills, the issue will be probably somewhere else. And most likely very trivial. Here is a list of 3 things you should check out.</p>
<p><strong>1. Return something for your Mock.</strong></p>
<p>Most likely, you mistyped returning function. You probably wanted to return the value for the mocked object. So instead of <code>when-thenReturn</code> , you might type just <code>when-then</code>. Maybe it was IntelliSense. Maybe you did it accidentally. But for sure, <code>NullPointerException</code> happened because you want something which is not there. Debug and check if you are returning something.</p>
<p><strong>2. Specify Mockito running class</strong></p>
<p>Don&#8217;t forget to annotate your Testing class with <code>@RunWith(MockitoJUnitRunner.class)</code>. Most of the people just forget to specify the test runner, running class for mocking.</p>
<p><strong>3. You need to annotate the mocking object with the <code>@Mock</code> annotation</strong></p>
<p>If you want to mock an object, you need to annotate the object with <code>@Mock</code> annotation.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codepills.com/3-basic-mistakes-for-nullpointerexception-when-mock/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
	</channel>
</rss>
