{"id":2756,"date":"2005-10-05T13:04:49","date_gmt":"2005-10-05T11:04:49","guid":{"rendered":"http:\/\/blog.jteam.nl\/?p=2756"},"modified":"2005-10-05T13:04:49","modified_gmt":"2005-10-05T11:04:49","slug":"easymock2-in-a-nutshell","status":"publish","type":"post","link":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/","title":{"rendered":"EasyMock2 in a Nutshell"},"content":{"rendered":"<p>The essential tool for unit testing, <a href=\"http:\/\/www.easymock.org\/\">EasyMock<\/a>, has come to a new level. This blog entry introduces the new EasyMock2 release.<br \/>\nWhile it is still in pre-release stage, I do recommend using EasyMock2 for all your Java 5 projects.<\/p>\n<p>To quote Tammo Freese (Project Founder) from the EasyMock project:<\/p>\n<blockquote><p>\n\u2018I plan no modifications other than bug fixes from this point.\u2019\n<\/p><\/blockquote>\n<p><!--more--><\/p>\n<h3>What\u2019s new<\/h3>\n<ol>\n<li>No more need for MockControl objects<\/li>\n<li>Build in argument matchers for most common argument matching (a lot less nuisance of having to write your own <code>ArgumentMatcher<\/code>)<\/li>\n<\/ol>\n<h3>Step-by-step example<\/h3>\n<p>Let\u2019s look at a step-by-step example of using EasyMock2:<\/p>\n<ol>\n<li>\nPut the following line in your imports:<\/p>\n<p><code>import <b>static<\/b> org.easymock.EasyMock.*;<\/code><\/p>\n<p>Note the static import, this imports all static members (methods in this case) from the EasyMock class.<br \/>\nMore on static imports here: <a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/guide\/language\/static-import.html\">http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/guide\/language\/static-import.html<\/a>.\n<\/li>\n<li>\nCreate a mock object using the following line (probably in your setup() method):<\/p>\n<p><code>myManager = createMock(MyManager.class);<\/code><\/p>\n<p>Note 1. It is still only possible to mock interfaces and NOT classes. However, using <a href=\"http:\/\/www.easymock.org\/EasyMock2_0_ClassExtension_Documentation.html\">EasyMock Class Extension<\/a> it is possible to mock classes.<br \/>\nNote 2. The <code>createMock()<\/code> method is a static method on the EasyMock class. It is still possible to use the methods <code>createStrictMock()<\/code> and <code>createNiceMock()<\/code> to respectively create a mock which also verifies the order in which the methods are called and a mock that allows all method calls and returns appropriate empty values by default.<br \/>\nImportant: do not forget to set the mocked object on the class under testing.\n<\/li>\n<li>\nSpecify behavior for the mock object. This example assumes we expect the method isDebug() to be called on our manager six times and returning false on each occasion:<\/p>\n<p><code>expect(myManager.isDebug()).andReturn(false).times(6);<\/code><\/p>\n<p>Note 1. Apart from <code>andReturn()<\/code>, you could also use <code>andThrow()<\/code> to throw an exception. You can also use <code>andStubReturn()<\/code> and <code>andStubThrow()<\/code> to specify default behavior.<br \/>\nNote2. In EasyMock2 it is easy to specify the number of times you expect the method to be called (in the example the <code>times(6)<\/code> call). Next to the <code>times(int)<\/code> method, there are a number of other convenience methods:<\/p>\n<p><code>times(int count)<\/code><br \/>\n<code>times(int min, int max)<\/code> &#8211; expect between min and max calls<br \/>\n<code>atLeastOnce()<\/code> &#8211; expect the method to be called at least once<br \/>\n<code>anyTimes()<\/code> &#8211; expect an unrestricted number of calls\n<\/li>\n<li>\nSet the mock in the replay state and verify after testing the class under test, similar to calling replay and verify on the MockControl object in the previous EasyMock version:<\/p>\n<p><code><i>replay<\/i>(myManager);<\/code><br \/>\n<code>classUnderTest.someMethod();<\/code><br \/>\n<code><i>verify<\/i>(myManager);<\/code>\n<\/li>\n<\/ol>\n<h3>Best new feature<\/h3>\n<p>Ok, so far so good, but I left out the best new feature (in my opinion).<\/p>\n<p>Remember a method being called with unknown or different arguments on each call. You had to write your own argument matcher. No more\u2026\u2026<br \/>\nNow, for example you can do the following:<\/p>\n<p><code>myManager.someMethod(eq(\u201canArgument\u201d),isA(Method.class),notNull());<\/code><\/p>\n<p>Note 1. The first argument is equivalent to just calling:<br \/>\n<code>myManager.someMethod(\"anArgument\"<\/code>&#8230;<br \/>\nNote 2. The second argument specifies that it expects an instance of the Method class<br \/>\nNote 3. The last argument is expected to not be <code>null<\/code><br \/>\nNote 4. Either all arguments of a call have to be given by specifying argument matchers, or none<\/p>\n<p>The list of build in argument matchers is pretty extensive, but ofcourse it is still possible to write your own <code>ArgumentMatcher<\/code><br \/>\nBelow is a list of all build in argument matchers (copied from the EasyMock2 documentation).<\/p>\n<blockquote><p>\n<code>eq(X value)<\/code><br \/>\nMatches if the actual value is equals the expected value. Available for all primitive types and for objects.<\/p>\n<p><code>anyBoolean()<\/code>, <code>anyByte()<\/code>, <code>anyChar()<\/code>, <code>anyDouble()<\/code>, <code>anyFloat()<\/code>, <code>anyInt()<\/code>, <code>anyLong()<\/code>, <code>anyObject()<\/code>, <code>anyShort()<\/code><br \/>\nMatches any value. Available for all primitive types and for objects.<\/p>\n<p><code>eq(X value, X delta)<\/code><br \/>\nMatches if the actual value is equal to the given value allowing the given delta. Available for <code>float<\/code> and<br \/>\n<code>double<\/code>.<\/p>\n<p><code>aryEq(X value)<\/code><br \/>\nMatches if the actual value is equal to the given value according to <code>Arrays.equals()<\/code>. Available for primitive and object arrays.<\/p>\n<p><code>isNull()<\/code><br \/>\nMatches if the actual value is <code>null<\/code>. Available for objects.<\/p>\n<p><code>notNull()<\/code><br \/>\nMatches if the actual value is not <code>null<\/code>. Available for objects.<\/p>\n<p><code>same(X value)<\/code><br \/>\nMatches if the actual value is the same as the given value. Available for objects.<\/p>\n<p><code>isA(Class clazz)<\/code><br \/>\nMatches if the actual value is an instance of the given class, or if it is in instance of a class that extends or implements the given class. Available for objects.<\/p>\n<p><code>lt(X value)<\/code>, <code>leq(X value)<\/code>, <code>geq(X value)<\/code>, <code>gt(X value)<\/code><br \/>\nMatches if the actual value is less than\/less or equal\/greater or equal\/greater than the given value. Available for all numeric primitive types.\n<\/p><\/blockquote>\n<p>Hope this blog entry will help you improve your unit testing! It has definitely reduced the amount of mock code in the tests that I have refactored to EasyMock2. Hopefully the final version will be released very soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The essential tool for unit testing, EasyMock, has come to a new level. This blog entry introduces the new EasyMock2 release. While it is still in pre-release stage, I do recommend using EasyMock2 for all your Java 5 projects. To quote Tammo Freese (Project Founder) from the EasyMock project: \u2018I plan no modifications other than [&hellip;]<\/p>\n","protected":false},"author":22,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[10],"tags":[119,11,120,121],"class_list":["post-2756","post","type-post","status-publish","format-standard","hentry","category-development","tag-easymock","tag-java","tag-testing","tag-testing-tools"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>EasyMock2 in a Nutshell - Trifork Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"EasyMock2 in a Nutshell - Trifork Blog\" \/>\n<meta property=\"og:description\" content=\"The essential tool for unit testing, EasyMock, has come to a new level. This blog entry introduces the new EasyMock2 release. While it is still in pre-release stage, I do recommend using EasyMock2 for all your Java 5 projects. To quote Tammo Freese (Project Founder) from the EasyMock project: \u2018I plan no modifications other than [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/\" \/>\n<meta property=\"og:site_name\" content=\"Trifork Blog\" \/>\n<meta property=\"article:published_time\" content=\"2005-10-05T11:04:49+00:00\" \/>\n<meta name=\"author\" content=\"Bram Smeets\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bram Smeets\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/\",\"url\":\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/\",\"name\":\"EasyMock2 in a Nutshell - Trifork Blog\",\"isPartOf\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#website\"},\"datePublished\":\"2005-10-05T11:04:49+00:00\",\"author\":{\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4c4fd4f94ab0974a08b2ee382fd3841\"},\"breadcrumb\":{\"@id\":\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/trifork.nl\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"EasyMock2 in a Nutshell\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/trifork.nl\/blog\/#website\",\"url\":\"https:\/\/trifork.nl\/blog\/\",\"name\":\"Trifork Blog\",\"description\":\"Keep updated on the technical solutions Trifork is working on!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/trifork.nl\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4c4fd4f94ab0974a08b2ee382fd3841\",\"name\":\"Bram Smeets\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8281f54c4a011d16a05764251cf11d26?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8281f54c4a011d16a05764251cf11d26?s=96&d=mm&r=g\",\"caption\":\"Bram Smeets\"},\"url\":\"https:\/\/trifork.nl\/blog\/author\/bram\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"EasyMock2 in a Nutshell - Trifork Blog","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:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/","og_locale":"en_US","og_type":"article","og_title":"EasyMock2 in a Nutshell - Trifork Blog","og_description":"The essential tool for unit testing, EasyMock, has come to a new level. This blog entry introduces the new EasyMock2 release. While it is still in pre-release stage, I do recommend using EasyMock2 for all your Java 5 projects. To quote Tammo Freese (Project Founder) from the EasyMock project: \u2018I plan no modifications other than [&hellip;]","og_url":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/","og_site_name":"Trifork Blog","article_published_time":"2005-10-05T11:04:49+00:00","author":"Bram Smeets","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Bram Smeets","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/","url":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/","name":"EasyMock2 in a Nutshell - Trifork Blog","isPartOf":{"@id":"https:\/\/trifork.nl\/blog\/#website"},"datePublished":"2005-10-05T11:04:49+00:00","author":{"@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4c4fd4f94ab0974a08b2ee382fd3841"},"breadcrumb":{"@id":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/trifork.nl\/blog\/easymock2-in-a-nutshell\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/trifork.nl\/blog\/"},{"@type":"ListItem","position":2,"name":"EasyMock2 in a Nutshell"}]},{"@type":"WebSite","@id":"https:\/\/trifork.nl\/blog\/#website","url":"https:\/\/trifork.nl\/blog\/","name":"Trifork Blog","description":"Keep updated on the technical solutions Trifork is working on!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/trifork.nl\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/a4c4fd4f94ab0974a08b2ee382fd3841","name":"Bram Smeets","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/trifork.nl\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8281f54c4a011d16a05764251cf11d26?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8281f54c4a011d16a05764251cf11d26?s=96&d=mm&r=g","caption":"Bram Smeets"},"url":"https:\/\/trifork.nl\/blog\/author\/bram\/"}]}},"_links":{"self":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/users\/22"}],"replies":[{"embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/comments?post=2756"}],"version-history":[{"count":0,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/posts\/2756\/revisions"}],"wp:attachment":[{"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/media?parent=2756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/categories?post=2756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trifork.nl\/blog\/wp-json\/wp\/v2\/tags?post=2756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}