{"id":4156,"date":"2021-02-18T15:37:07","date_gmt":"2021-02-18T15:37:07","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4156"},"modified":"2021-02-20T06:47:24","modified_gmt":"2021-02-20T06:47:24","slug":"behaviour-driven-testing-in-mockito","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/behaviour-driven-testing-in-mockito\/","title":{"rendered":"Behaviour Driven Testing in Mockito"},"content":{"rendered":"<p><strong>What is BDD?<\/strong><\/p>\n<pre>\r\n  Given some preconditions (Arrange) -> When an action occurs (Act) -> Then verify the output (Assert)\r\n<\/pre>\n<p>I.E. <\/p>\n<pre>\r\n  Given three items in Cart\r\n  When one Item is deleted\r\n  Then Two items should be left in Cart\r\n<\/pre>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@ExtendWith(MockitoExtension.class)\r\npublic class EmpServiceImplTest {\r\n\r\n    @Mock\r\n    EmployeeDB employeeDB;\r\n\r\n    @InjectMocks\r\n    EmpServiceImpl empServiceImpl;\r\n\r\n    @Test\r\n    @DisplayName(&quot;Check Employee in BDD Style&quot;)\r\n    public void test_getEmployees_Success(){\r\n        \/\/Given - Employee List has only one row with List size as 1\r\n        \/\/When  - New Employee is added to the List\r\n        \/\/Then  -  Employee List should contain two rows\r\n\r\n        \/\/Given - Employee List has 3 Employees with List size as 3\r\n        List&lt;Employee&gt; arrEmployees = new ArrayList&lt;&gt;();\r\n        arrEmployees.add(new Employee(&quot;101&quot;, &quot;Mugil&quot;));\r\n        arrEmployees.add(new Employee(&quot;102&quot;, &quot;Max&quot;));\r\n        arrEmployees.add(new Employee(&quot;103&quot;, &quot;Mani&quot;));\r\n\r\n        given(employeeDB.getEmployeesFromDB()).willReturn(arrEmployees);\r\n\r\n        \/\/When\r\n        List&lt;Employee&gt; arrFilteredEmps = empServiceImpl.getEmployees();\r\n\r\n        \/\/Then - there should 3 EmployeegetEmployeeByIdFromDB\r\n        Assertions.assertEquals(3, arrFilteredEmps.size());\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is BDD? Given some preconditions (Arrange) -> When an action occurs (Act) -> Then verify the output (Assert) I.E. Given three items in Cart When one Item is deleted Then Two items should be left in Cart @ExtendWith(MockitoExtension.class) public class EmpServiceImplTest { @Mock EmployeeDB employeeDB; @InjectMocks EmpServiceImpl empServiceImpl; @Test @DisplayName(&quot;Check Employee in BDD Style&quot;)&hellip; <a href=\"https:\/\/codethataint.com\/blog\/behaviour-driven-testing-in-mockito\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[324],"tags":[],"class_list":["post-4156","post","type-post","status-publish","format-standard","hentry","category-mockito"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4156"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4156\/revisions"}],"predecessor-version":[{"id":4162,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4156\/revisions\/4162"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}