{"id":4411,"date":"2021-07-03T09:48:34","date_gmt":"2021-07-03T09:48:34","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4411"},"modified":"2021-07-04T14:45:42","modified_gmt":"2021-07-04T14:45:42","slug":"mock-vs-injectmock-vs-spy","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/mock-vs-injectmock-vs-spy\/","title":{"rendered":"@Mock vs @InjectMock vs @Spy"},"content":{"rendered":"<p>Below is a class StudentUtils which contains only two Method &#8211; doNothingWhenCalled and dummymethod. We are going to unit test doNothingWhenCalled method.<\/p>\n<p><em>The conclusion would be to use @Spy when we mock methods of same class tested to doNothing and use @Mock to mock method to doNothing call to other classes.<\/em><\/p>\n<p><strong>StudentUtils.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage com.mugil.org;\r\n\r\nimport java.util.ArrayList;\r\nimport java.util.List;\r\n\r\npublic class StudentUtils {\r\n    private List arrStudents = new ArrayList();\r\n    private String Name = &quot;Mugil&quot;;\r\n\r\n    public StudentUtils(){\r\n        arrStudents.add(new Student(&quot;101&quot;, &quot;Mugil&quot;));\r\n    }\r\n\r\n    public Student doNothingWhenCalled()\r\n    {\r\n        dummymethod(new Student(&quot;102&quot; ,&quot;abc&quot;));\r\n        return new Student(&quot;103&quot;, &quot;StudentName&quot;);\r\n    }\r\n\r\n    public String dummymethod(Student objStudent)\r\n    {\r\n        System.out.println(&quot;Dummy Method Called&quot;);\r\n        return &quot;Dummy Method&quot;;\r\n    }\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">@InjectMocks<\/strong><br \/>\n<strong>StudentUtilsTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@ExtendWith({MockitoExtension.class})\r\nclass StudentUtilsTest {\r\n    @InjectMocks\r\n    StudentUtils systemUnderTest;\r\n\r\n    @Test\r\n    void doNothingCalled() {\r\n        Assertions.assertThat(systemUnderTest.doNothingWhenCalled()).isInstanceOf(Student.class);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nDummy Method Called\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">@Mocks<\/strong><br \/>\n<strong>StudentUtilsTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@ExtendWith({MockitoExtension.class})\r\nclass StudentUtilsTest {\r\n    @InjectMocks\r\n    StudentUtils systemUnderTest;\r\n\r\n    @Test\r\n    void doNothingCalled() {\r\n        systemUnderTest = Mockito.mock(StudentUtils.class);\r\n        Mockito.when(systemUnderTest.dummymethod(any())).thenReturn(&quot;Dummy Method&quot;);\r\n\r\n        Assertions.assertThat(systemUnderTest.doNothingWhenCalled()).isInstanceOf(Student.class);\r\n    }\r\n}\r\n<\/pre>\n<p>You can see below we are trying to call method to be tested under empty mock instance which doesnot have default method definition. There is no point in mocking method definition since <strong>systemUnderTest.doNothingWhenCalled()<\/strong> would try to invoke test method which has no idea about.<br \/>\n<strong>Output<\/strong><\/p>\n<pre>\r\njava.lang.AssertionError: \r\nExpecting actual not to be null\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">@Spy<\/strong><br \/>\n<strong>StudentUtilsTest.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@ExtendWith({MockitoExtension.class})\r\nclass StudentUtilsTest {\r\n    @InjectMocks\r\n    StudentUtils systemUnderTest;\r\n\r\n    @Test\r\n    void doNothingCalled() {\r\n        systemUnderTest = Mockito.spy(StudentUtils.class);\r\n        Assertions.assertThat(systemUnderTest.doNothingWhenCalled()).isInstanceOf(Student.class);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nDummy Method Called\r\n<\/pre>\n<p>Below is the Debug of Object created using @Mock, @InjectMock and @Spy.As you can see the one with Mock contains only MockInterceptor which does not have definition of methods on its own where as @InjectMock created instance of TestClass. @Spy contains both MockInterceptor and Instance enabling partial mock.<\/p>\n<p>While Using InjectMock you can see the MockitoInterceptor in scope<br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/07\/Mock.png\" alt=\"\" \/><\/p>\n<p>While Using InjectMock you can see the class variables getting initialized<br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/07\/InjectMock.png\" alt=\"\" \/><\/p>\n<p>While Using Spy you can see both class variables getting initialized and MockitoInterceptor in scope<br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2021\/07\/Spy.png\" alt=\"\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is a class StudentUtils which contains only two Method &#8211; doNothingWhenCalled and dummymethod. We are going to unit test doNothingWhenCalled method. The conclusion would be to use @Spy when we mock methods of same class tested to doNothing and use @Mock to mock method to doNothing call to other classes. StudentUtils.java package com.mugil.org; import&hellip; <a href=\"https:\/\/codethataint.com\/blog\/mock-vs-injectmock-vs-spy\/\">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":[335],"class_list":["post-4411","post","type-post","status-publish","format-standard","hentry","category-mockito","tag-mockito"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4411","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=4411"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4411\/revisions"}],"predecessor-version":[{"id":4422,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4411\/revisions\/4422"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}