{"id":2118,"date":"2011-08-30T09:36:00","date_gmt":"2011-08-30T07:36:00","guid":{"rendered":""},"modified":"2023-03-22T12:16:16","modified_gmt":"2023-03-22T11:16:16","slug":"a-mockito-catch","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2011\/08\/30\/a-mockito-catch\/","title":{"rendered":"A Mockito catch"},"content":{"rendered":"<p>Suppose we have such classes and interfaces<\/p>\n<p><em>public class AddOrganizationAction implements Action {}<\/em><br \/>\n<em>public class AddPersonToOrganizationAction implements Action {}<\/em><br \/>\n*<br \/>\n*<br \/>\n<em>public interface DispatchAsync {<br \/>\n\u00a0\u00a0\u00a0\u00a0 void execute( Action action, AsyncCallback callback );<br \/>\n}<\/em><\/p>\n<p>We&#8217;re using the <a href=\"http:\/\/www.mockito.org\/\">best mocking framework<\/a> ;) Suppose we want to verify that code under test will call* execute()* with proper Action &#8211; <em>AddOrganizationAction<\/em>.<br \/>\nI found that many developers (including me!) check such condition with<\/p>\n<p><em>verify(async).execute(any(AddOrganizationAction.class), any(AsyncCallback.class));<\/em><\/p>\n<p>In such case *AsyncCallback *is not important for us. We just want to ensure that *AddOrganizationAction *will be passed. We run test and it&#8217;s green. But suddenly if we put the code below into test it will be green too!<\/p>\n<p><em>verify(async).execute(any(AddPersonToOrganizationAction .class), any(AsyncCallback.class));<\/em><\/p>\n<p>Why? Because <a href=\"http:\/\/docs.mockito.googlecode.com\/hg\/latest\/org\/mockito\/Matchers.html#any(java.lang.Class)\"><em>any()<\/em> matcher<\/a> doesn&#8217;t check the instance of passed object to be equal to declared class (<em>AddOrganizationAction<\/em> in this case). <em>Any()<\/em> checks if passed object conforms to method signature. In this case any <em>Action<\/em>&#8216;s child will do. And we have an erroneous test!<br \/>\nThe proper matcher we&#8217;d like to use is <a href=\"http:\/\/docs.mockito.googlecode.com\/hg\/latest\/org\/mockito\/Matchers.html#isA(java.lang.Class)\"><em>isA()<\/em><\/a> matcher that checks if passed object is instance of declared class (which means instance of class or it&#8217;s children).<\/p>\n<p>So the proper test should contain<\/p>\n<p><em>verify(async).execute(isA(<strong>AddOrganizationAction<\/strong>.class), any(AsyncCallback.class));<\/em><\/p>\n<p>Go now and search for <em>any()<\/em> usages and think about changing it to* isA()*. In 1 of 10 cases whenever I change all tests in a testcase to <em>isA()<\/em> usage, I find an error in implementation of logic under test. Luckily I know the catch and now you do :)<\/p>\n<p>Ok. It&#8217;s not really a catch but ignorance of all us developers that we don&#8217;t read <a href=\"http:\/\/docs.mockito.googlecode.com\/hg\/latest\/org\/mockito\/Mockito.html\"><strong>entire<\/strong> documentantation<\/a> :)<\/p>\n<div class=\"blogger-post-footer\">\n  <img width='1' height='1' src='https:\/\/blogger.googleusercontent.com\/tracker\/3430508731490960624-556436253915532035?l=blog.bartekzdanowski.pl' alt='' \/>\n<\/div>\n","protected":false},"excerpt":{"rendered":"Suppose we have such classes and interfaces public class AddOrganizationAction implements Action {} public class AddPersonToOrganizationAction implements Action&hellip;\n","protected":false},"author":22,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[30],"class_list":{"0":"post-2118","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-testing"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/2118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=2118"}],"version-history":[{"count":7,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/2118\/revisions"}],"predecessor-version":[{"id":15471,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/2118\/revisions\/15471"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}