{"id":5843,"date":"2018-04-11T21:35:23","date_gmt":"2018-04-11T21:35:23","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5843"},"modified":"2018-04-15T11:15:27","modified_gmt":"2018-04-15T11:15:27","slug":"using-jmock","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/using-jmock\/","title":{"rendered":"Using JMock"},"content":{"rendered":"<p>At some point we&#8217;re likely to require a mocking framework for our unit test code. Ofcourse there&#8217;s several java based frameworks. In this post I&#8217;m going to look into using JMock.<\/p>\n<p><strong>Setting up an example<\/strong><\/p>\n<p>Let&#8217;s start by looking at an interface, my old favourite a Calculator <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface Calculator {\r\n    double add(double a, double b);\r\n}\r\n<\/pre>\n<p>Let&#8217;s now add some dependencies to the pom.xml<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;dependencies&gt;\r\n   &lt;dependency&gt;\r\n      &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n      &lt;version&gt;4.12&lt;\/version&gt;\r\n      &lt;scope&gt;test&lt;\/scope&gt;\r\n   &lt;\/dependency&gt;\r\n   &lt;dependency&gt;\r\n      &lt;groupId&gt;org.jmock&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;jmock-junit4&lt;\/artifactId&gt;\r\n      &lt;version&gt;2.8.4&lt;\/version&gt;\r\n      &lt;scope&gt;test&lt;\/scope&gt;\r\n   &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n<\/pre>\n<p><strong>Using JMock<\/strong><\/p>\n<p>We need to start off by creating a <em>Mockery<\/em> object. This will be used to create the mocks as well as handle the three A&#8217;s, Arrange, Act and Assert. Let&#8217;s jump straight in an look at some code&#8230;<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.putridparrot;\r\n\r\nimport org.jmock.Expectations;\r\nimport org.jmock.Mockery;\r\nimport org.junit.After;\r\nimport org.junit.Before;\r\nimport org.junit.Test;\r\n\r\nimport static org.junit.Assert.assertEquals;\r\n\r\npublic class CalculatorTest {\r\n\r\n    private Mockery mockery;\r\n\r\n    @Before\r\n    public void setUp() {\r\n        mockery = new Mockery();\r\n    }\r\n\r\n    @After\r\n    public void tearDown() {\r\n        mockery.assertIsSatisfied();\r\n    }\r\n\r\n    @Test\r\n    public void add() {\r\n\r\n        final Calculator calc = mockery.mock(Calculator.class);\r\n\r\n        mockery.checking(new Expectations()\r\n        {\r\n            {\r\n                oneOf(calc).add(2.0, 4.0);\r\n                will(returnValue(6.0));\r\n            }\r\n        });\r\n\r\n        double result = calc.add(2, 4);\r\n        assertEquals(6.0, result, 0);\r\n    }\r\n}\r\n<\/pre>\n<p>In the code above we&#8217;re creating the <em>Mockery<\/em> in the <em>setup<\/em> method and the assert in the <em>teardown<\/em>.<\/p>\n<p>We then use the <em>Mockery<\/em> to create a mock of the interface Calculator which we would normally pass into some other class to use, but for simplicity I&#8217;ve simply demonstrated how we arrange the mock using <em>mockery.checking<\/em> along with the expectations. Then we act on the mock object by calling the <em>add<\/em> method which ofcourse will execute our arrange code.<\/p>\n<p>Here&#8217;s an example of some code which demonstrates arranging values which will return a different value each time, so we&#8217;ll add the following to our Calculator interface<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nint random();\r\n<\/pre>\n<p>and now create the following test, which expects <em>random<\/em> to be called three times and arranges each return accordingly.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Test\r\npublic void random() {\r\n   final Calculator calc = mockery.mock(Calculator.class);\r\n\r\n   mockery.checking(new Expectations()\r\n   {\r\n      {\r\n         exactly(3)\r\n            .of(calc)\r\n            .random();\r\n\r\n         will(onConsecutiveCalls(\r\n            returnValue(4),\r\n            returnValue(10),\r\n            returnValue(42)\r\n         ));\r\n      }\r\n   });\r\n\r\n   assertEquals(4, calc.random());\r\n   assertEquals(10, calc.random());\r\n   assertEquals(42, calc.random());\r\n}\r\n<\/pre>\n<p>Ofcourse there&#8217;s plenty more to JMock, but this should get you started.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point we&#8217;re likely to require a mocking framework for our unit test code. Ofcourse there&#8217;s several java based frameworks. In this post I&#8217;m going to look into using JMock. Setting up an example Let&#8217;s start by looking at an interface, my old favourite a Calculator public interface Calculator { double add(double a, double [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[161,213],"tags":[],"class_list":["post-5843","post","type-post","status-publish","format-standard","hentry","category-java","category-jmock"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5843"}],"version-history":[{"count":4,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5843\/revisions"}],"predecessor-version":[{"id":6174,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5843\/revisions\/6174"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}