{"id":192,"date":"2012-07-31T10:57:19","date_gmt":"2012-07-31T17:57:19","guid":{"rendered":"http:\/\/www.lorrin.org\/blog\/?p=192"},"modified":"2012-07-31T10:57:19","modified_gmt":"2012-07-31T17:57:19","slug":"mocking-java-final-classes","status":"publish","type":"post","link":"https:\/\/www.lorrin.org\/blog\/2012\/07\/31\/mocking-java-final-classes\/","title":{"rendered":"Mocking Java final classes"},"content":{"rendered":"<p>Trying to unit test some code that was to run inside Solr, I bumped into this:<\/p>\n<pre>Cannot mock\/spy class org.apache.solr.core.SolrCore\r\nMockito cannot mock\/spy following:\r\n\u00a0 - final classes\r\n\u00a0 - anonymous classes\r\n\u00a0 - primitive types<\/pre>\n<p>Fortunately, there&#8217;s a simple solution: <a href=\"https:\/\/code.google.com\/p\/powermock\/\">PowerMock<\/a>. After adding the following two annotations to my test class definition (and the <a href=\"https:\/\/code.google.com\/p\/powermock\/wiki\/Mockito_maven\">requisite Maven dependency declarations<\/a>), everything just worked. No changes needed to the actual Mockito calls themselves. Sweet.<\/p>\n<pre class=\"brush:java\">@RunWith(PowerMockRunner.class)\r\n@PrepareForTest( { SolrCore.class })<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Trying to unit test some code that was to run inside Solr, I bumped into this: Cannot mock\/spy class org.apache.solr.core.SolrCore Mockito cannot mock\/spy following: \u00a0 &#8211; final classes \u00a0 &#8211; anonymous classes \u00a0 &#8211; primitive types Fortunately, there&#8217;s a simple solution: PowerMock. After adding the following two annotations to my test class definition (and the <a href='https:\/\/www.lorrin.org\/blog\/2012\/07\/31\/mocking-java-final-classes\/' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[48],"tags":[64,62,61,63,65],"_links":{"self":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/192"}],"collection":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":4,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lorrin.org\/blog\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}