{"id":875,"date":"2022-07-23T09:05:26","date_gmt":"2022-07-20T11:51:37","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/uncategorized\/expect-the-easymock-void-method-to-have-its-last-call\/"},"modified":"2024-03-06T14:43:15","modified_gmt":"2024-03-06T14:43:15","slug":"expect-the-easymock-void-method-to-have-its-last-call","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/","title":{"rendered":"EasyMock void method to have its last call."},"content":{"rendered":"<p>Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with andAnswer() to successfully mock void methods.<\/p>\n<h2>Create a void method in EasyMock.<\/h2>\n<p>If we utilize expectLastCall() along with andAnswer() for mocking void methods, we can utilize getCurrentArguments() to retrieve the arguments passed to the method and carry out certain actions on it. Ultimately, we need to return null since we are simulating a void method. Suppose we possess a utility class like the following:<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.utils;\r\n\r\npublic class StringUtils {\r\n\r\n\tpublic void print(String s) {\r\n\t\tSystem.out.println(s);\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>EasyMock provides the code to mimic the print() function of a void method.<\/p>\n<pre class=\"post-pre\"><code>package com.scdev.easymock;\r\n\r\nimport static org.easymock.EasyMock.*;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport com.scdev.utils.StringUtils;\r\n\r\npublic class EasyMockVoidMethodExample {\r\n\r\n  @Test\r\n  public void test() {\r\n    StringUtils mock = mock(StringUtils.class);\r\n    \r\n    mock.print(anyString());\r\n    expectLastCall().andAnswer(() -&gt; {\r\n      System.out.println(\"Mock Argument = \"\r\n          +getCurrentArguments()[0]);\r\n      return null;\r\n    }).times(2);\r\n    replay(mock);\r\n    \r\n    mock.print(\"Java\");\r\n    mock.print(\"Python\");\r\n    verify(mock);\r\n  }\r\n}\r\n<\/code><\/pre>\n<div><img decoding=\"async\" class=\"post-images\" title=\"\" src=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cec7dc40ba52feef2b87d\/6-0.png\" alt=\"EasyMock void method\" \/><\/div>\n<h3>We anticipate the last call to be void.<\/h3>\n<p>For mocking a void method without executing any logic, we can utilize expectLastCall().andVoid() immediately after invoking the void method on the mocked object.<\/p>\n<p>Our <a href=\"https:\/\/github.com\/easymock\/easymock\">GitHub Repository<\/a> contains a comprehensive project and additional EasyMock examples that you can explore and access.<\/p>\n<p>&nbsp;<\/p>\n<p>more tutorials<\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/java-string-substring-method\/\" target=\"_blank\" rel=\"noopener\">Java String substring() method<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/spring-mvc-handlerinterceptoradapter-and-handlerinterceptor\/\" target=\"_blank\" rel=\"noopener\">Spring MVC HandlerInterceptorAdapter and HandlerInterceptor.<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p><a class=\"LinkSuggestion__Link-sc-1gewdgc-4 cLBplk\" href=\"https:\/\/www.silicloud.com\/blog\/broadcastreceiver-android-tutor\/\" target=\"_blank\" rel=\"noopener\">BroadcastReceiver Example Tutorial on Android<span class=\"sc-gswNZR eASTkv\">(Opens in a new browser tab)<\/span><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with andAnswer() to successfully mock void methods. Create a void method in EasyMock. If we utilize expectLastCall() along with andAnswer() for mocking void methods, we can utilize getCurrentArguments() to retrieve the arguments passed to the method [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>EasyMock void method to have its last call. - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"EasyMock void method to have its last call.\" \/>\n<meta property=\"og:description\" content=\"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-20T11:51:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-06T14:43:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cec7dc40ba52feef2b87d\/6-0.png\" \/>\n<meta name=\"author\" content=\"Benjamin Taylor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benjamin Taylor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\"},\"author\":{\"name\":\"Benjamin Taylor\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\"},\"headline\":\"EasyMock void method to have its last call.\",\"datePublished\":\"2022-07-20T11:51:37+00:00\",\"dateModified\":\"2024-03-06T14:43:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\"},\"wordCount\":190,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\",\"name\":\"EasyMock void method to have its last call. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2022-07-20T11:51:37+00:00\",\"dateModified\":\"2024-03-06T14:43:15+00:00\",\"description\":\"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"EasyMock void method to have its last call.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9\",\"name\":\"Benjamin Taylor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g\",\"caption\":\"Benjamin Taylor\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"EasyMock void method to have its last call. - Blog - Silicon Cloud","description":"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/","og_locale":"en_US","og_type":"article","og_title":"EasyMock void method to have its last call.","og_description":"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with","og_url":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2022-07-20T11:51:37+00:00","article_modified_time":"2024-03-06T14:43:15+00:00","og_image":[{"url":"https:\/\/cdn.silicloud.com\/blog-img\/blog\/img\/655cec7dc40ba52feef2b87d\/6-0.png"}],"author":"Benjamin Taylor","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Benjamin Taylor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/"},"author":{"name":"Benjamin Taylor","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9"},"headline":"EasyMock void method to have its last call.","datePublished":"2022-07-20T11:51:37+00:00","dateModified":"2024-03-06T14:43:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/"},"wordCount":190,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/","url":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/","name":"EasyMock void method to have its last call. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2022-07-20T11:51:37+00:00","dateModified":"2024-03-06T14:43:15+00:00","description":"Mocking void methods using the EasyMock expect() method is not possible. However, we can instead use expectLastCall() in conjunction with","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/expect-the-easymock-void-method-to-have-its-last-call\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"EasyMock void method to have its last call."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/ac801fe9549a25960ce48aa2e0a691c9","name":"Benjamin Taylor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ec2e3d3e2d525fd148047c4520ae7c1cdccd1f4b48a1a488422b31f04f345c14?s=96&d=mm&r=g","caption":"Benjamin Taylor"},"url":"https:\/\/www.silicloud.com\/blog\/author\/benjamintaylor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=875"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions\/1691"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}