Using MockitoJUnitRunner.class instead of SpringJUnit4ClassRunner.class

"[email protected] [junit]" <[email protected]> 28 Jun 2015 08:19:10 -0700
Newsgroups gmane.comp.java.junit.user
Message-ID <[email protected]>
--9nlWnTS9ocNmxdhsK3FKVLWiM6a5jwOaA8HvrLV
Content-Transfer-Encoding: base64
Content-Type: text/plain; charset="UTF-8"

SSBoYXZlIGEgcXVlc3Rpb24gYWJvdXQgdGhlIHVzYWdlIG9mIFNwcmluZ0pVbml0NENsYXNzUnVu
bmVyLmNsYXNzLiBGb3IgcHVyZSBKdW5pdHMgb3IgVW5pdCBUZXN0IGNhc2VzIHNob3VsZCB3ZSB1
c2UgU3ByaW5nIGJhc2VkIGFubm90YXRpb25zIHN1Y2ggYXMgQEF1dG93aXJlZCBhbG9uZyB3aXRo
IFNwcmluZ0pVbml0NENsYXNzUnVubmVyLmNsYXNzIG9yIHNob3VsZCB3ZSB1c2UgQFJ1bldpdGgo
Ik1vY2tpdG9KVW5pdFJ1bm5lci5jbGFzcyIpIGFubm90YXRpb24gaW5zdGVhZCB3aXRoIHRoZSAg
YXQgdGhlIHRvcCBvZiB0aGUgVGVzdCBjbGFzcz8NCiANCg0KIEkgbWVhbiByZXBsYWNpbmcgQFJ1
bldpdGgoU3ByaW5nSlVuaXQ0Q2xhc3NSdW5uZXIuY2xhc3MpDQogQENvbnRleHRDb25maWd1cmF0
aW9uKHsgImNsYXNzcGF0aDp0ZXN0LWFwcGxpY2F0aW9uQ29udGV4dC54bWwiIH0pIHdpdGgganVz
dCBAUnVuV2l0aChNb2NraXRvSlVuaXRSdW5uZXIuY2xhc3MgYXQgdGhlIHRvcCBvZiB0aGUgY2xh
c3MuIEl0IHdvcmtzIGZvciBtZS4NCiANCg0KIE15IHVuZGVyc3RhbmRpbmcgaXMgLCBJbiBKdW5p
dHMgd2Ugbm9ybWFsbHkgZG8gbm90IG1ha2UgYW55IGV4dGVybmFsIGNhbGxzIHN1Y2ggYXMgY2Fs
bHMgdG8gREIgb3IgYS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aXRzPw0KIA0KDQog
DQogU3VnZ2VzdGlvbnMgcGxlYXNlLg0K


--9nlWnTS9ocNmxdhsK3FKVLWiM6a5jwOaA8HvrLV
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit





<head>

<style type="text/css">
<!--

/* start of attachment style */
       .ygrp-photo-title{
         clear: both;
         font-size: smaller;
         height: 15px;
         overflow: hidden;
         text-align: center;
         width: 75px;
       }
       div.ygrp-photo{
         background-position: center;
         background-repeat: no-repeat;
         background-color: white;
         border: 1px solid black;
         height: 62px;
         width: 62px;
       }

       div.photo-title 
         a,
         div.photo-title a:active,
         div.photo-title a:hover,
         div.photo-title a:visited {
           text-decoration: none; 
       }

       div.attach-table div.attach-row {
         clear: both;
       }

       div.attach-table div.attach-row div {
         float: left;
         /* margin: 2px;*/
       }

       p {
         clear: both;
         padding: 15px 0 3px 0;
	 overflow: hidden;
       }

       div.ygrp-file {
         width: 30px;
         valign: middle;
       }
       div.attach-table div.attach-row div div a {
         text-decoration: none;
       }

       div.attach-table div.attach-row div div span {
         font-weight: normal;
       }

       div.ygrp-file-title {
         font-weight: bold;
       }
 /* end of attachment style */
        -->
        </style>
        </head>
<html>
<head>
<style type="text/css">
<!--
#ygrp-mkp {
  border: 1px solid #d8d8d8;
  font-family: Arial;
  margin: 10px 0;
  padding: 0 10px;
}

#ygrp-mkp hr {
  border: 1px solid #d8d8d8;
}

#ygrp-mkp #hd {
  color: #628c2a;
  font-size: 85%;
  font-weight: 700;
  line-height: 122%;
  margin: 10px 0;
}

#ygrp-mkp #ads {
  margin-bottom: 10px;
}

#ygrp-mkp .ad {
  padding: 0 0;
}

#ygrp-mkp .ad p {
  margin: 0;
}

#ygrp-mkp .ad a {
  color: #0000ff;
  text-decoration: none;
}
-->
</style>
</head>
<body>



<!-- |**|begin egp html banner|**| -->

<br><br>

<!-- |**|end egp html banner|**| -->


<p>I have a question about the usage of SpringJUnit4ClassRunner.class. For pure Junits or Unit Test cases should we use Spring based annotations such as @Autowired along with SpringJUnit4ClassRunner.class or should we use&nbsp;<span style="word-spacing:normal;line-height:1.25;">@RunWith(</span><span style="word-spacing:normal;line-height:1.25;">&quot;MockitoJUnitRunner.class&quot;) annotation instead with the &nbsp;at the top of the Test class?</span></p><p><br></p><p>I mean replacing @RunWith(SpringJUnit4ClassRunner.class)</p><p>@ContextConfiguration({ &quot;classpath:test-applicationContext.xml&quot; }) with just <span style="font-weight:bold;">@RunWith(MockitoJUnitRunner.class</span> at the top of the class. It works for me.</p><p><br></p><p>My understanding is , In Junits we normally d
 o not make any external calls such as calls to DB or a call to some other web service that are outside of the class that is being tested. We have to mock these external calls using <span style="font-weight:bold;">@Mock</span> annotations on this service objects. And then create a real object of the class that we are testing &amp; that depends on these mocks. We can then use <span style="font-weight:bold;">@InjectMocks</span> on the real object so that it will be injected with the mocked objects.</p><p><br></p><p>Example :&nbsp;<span style="font-weight:bold;">Service-A-&gt;Calls-&gt;Service-B-&gt;Calls-&gt;Service-C</span></p><p><br></p><p>While testing Service-A we should mock Service B &amp; while testing Service-B we should mock Service-C. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p><p
 >So, I feel for Unit test cases we need not rely on the dependency injection of the Spring container to provide us the instance of the class we are testing.</p><p><br></p><p>Or is it fine to use any one of these techniques to write Junits?</p><p><br></p><p><span></span></p><p>Suggestions please.</p>



<!-- |**|begin egp html banner|**| -->

<br>


<br>

<!-- |**|end egp html banner|**| -->


<div width="1" style="color: white; clear: both;"/>__._,_.___</div>

      
  

    
    <div id="fromDMARC" style="clear:both; margin-top: 10px;">
         <hr style="height:2px ; border-width:0; color:#E3E3E3; background-color:#E3E3E3;">
         Posted by: [email protected]         <hr style="height:2px ; border-width:0; color:#E3E3E3; background-color:#E3E3E3;">
    </div>
<!-- Start Recommendations -->
<!-- End Recommendations -->



<!-- |**|begin egp html banner|**| -->

  <img src="http://geo.yahoo.com/serv?s=97476590/grpId=2423328/grpspId=1705006905/msgId=24681/stime=1436422331" width="1" height="1"> <br>

<!-- |**|end egp html banner|**| -->

  
<!-- |**|begin egp html banner|**| -->

<br>



   
     
 
        <!-- |**|begin egp html banner|**| -->
        <div id="ygrp-vital" style="background-color: #f2f2f2; font-family: Verdana; font-size: 10px; margin-bottom: 10px; padding: 10px;">

        <span id="vithd" style="font-weight: bold; color: #333; text-transform: uppercase; "><a href="https://groups.yahoo.com/neo/groups/junit/info;_ylc=X3oDMTJldXF0YWc0BF9TAzk3MzU5NzE0BGdycElkAzI0MjMzMjgEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwN2dGwEc2xrA3ZnaHAEc3RpbWUDMTQzNjQyMjMzMQ--" style="text-decoration: none;">Visit Your Group</a></span>

     <ul style="list-style-type: none; margin: 0; padding: 0; display: inline;">
                                                    </ul>
  </div>


<div id="ft" style="font-family: Arial; font-size: 11px; margin-top: 5px; padding: 0 2px 0 0; clear: both;">
  <a href="https://groups.yahoo.com/neo;_ylc=X3oDMTJkdGczaGsxBF9TAzk3NDc2NTkwBGdycElkAzI0MjMzMjgEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwNmdHIEc2xrA2dmcARzdGltZQMxNDM2NDIyMzMx" style="float: left;"><img src="http://l.yimg.com/ru/static/images/yg/img/email/new_logo/logo-groups-137x15.png" height="15" width="137" alt="Yahoo! Groups" style="border: 0;"/></a>
  <div style="color: #747575; float: right;"> &bull; <a href="https://info.yahoo.com/privacy/us/yahoo/groups/details.html" style="text-decoration: none;">Privacy</a> &bull; <a href="mailto:[email protected]?subject=Unsubscribe" style="text-decoration: none;">Unsubscribe</a> &bull; <a href="https://info.yahoo.com/legal/us/yahoo/utos/terms/" style="text-decoration: none;">Terms of Use</a> </div>
</div>

<!-- |**|end egp html banner|**| -->

  </div> <!-- ygrp-msg -->

   


  <br>

<!-- |**|end egp html banner|**| -->


<div style="color: white; clear: both;"/>__,_._,___</div>
</body>

</html>

--9nlWnTS9ocNmxdhsK3FKVLWiM6a5jwOaA8HvrLV--