Using MockitoJUnitRunner.class instead of SpringJUnit4ClassRunner.class

"Ayaskant Swain [email protected] [junit]" <[email protected]> Sun, 28 Jun 2015 21:28:13 +0530
Newsgroups gmane.comp.java.junit.user
Message-ID <CAJo85JZ40jOpZFHSCup78yYAAqWgqJnd0JY9OGXGZXG3WXPzgQ@mail.gmail.com>
--001a113f9b7663da9e0519960a86
Content-Type: text/plain; charset=UTF-8

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
"MockitoJUnitRunner.class" instead with the @RunWith annotation at the top
of the Test class?

I mean replacing @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-applicationContext.xml" }) with
just @RunWith(MockitoJUnitRunner.class) at the top of the class. It works
for me.

In Junits we normally do not make any external calls such as calls to DB or
call to some other web service. We have to mock these external calls using
@Mock annotations on this service objects. And then create a real object of
the class that we are testing & that depends on these mocks. We can then
use @InjectMocks on the real object so that it will be injected with the
mocked objects.

Example Service-A->Calls->Service-B->Calls->Service-C

While testing A we should mock Service B & while testing Service-B we
should mock Service-C.

Some code snippet -

@RunWith(MockitoJUnitRunner.class)
public class TestServiceA {

@Mock
B mockObj;

@InjectMocks
A realObj;

        private String accId = "a1234";

         @Test
         public void testServiceA() {
                   Account acc = new Account("a1234", "John", "India");
                   when(mockObj.getAccDetails(anyString()).thenReturn(acc);
                   Account accountObj = realObj.getCustomerAccount(accId);
                   assertNotNull(accountObj);
         }
}

So, I feel for Unit test cases we need not rely on Spring container to
provide us the instance of the class we are testing.

I might be wrong. Please give your suggestions.

--001a113f9b7663da9e0519960a86
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable





<head>

<style type=3D"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=20
         a,
         div.photo-title a:active,
         div.photo-title a:hover,
         div.photo-title a:visited {
           text-decoration: none;=20
       }

       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=3D"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|**| -->


<div dir=3D"ltr"><div>I have a question about the usage of SpringJUnit4Clas=
sRunner.class. For pure Junits or Unit Test cases should we use Spring base=
d annotations such as @Autowired along with SpringJUnit4ClassRunner.class o=
r should we use &quot;MockitoJUnitRunner.class&quot; instead with the @RunW=
ith annotation at the top of the Test class?</div><div><br></div><div>I mea=
n replacing @RunWith(SpringJUnit4ClassRunner.class)</div><div>@ContextConfi=
guration({ &quot;classpath:test-applicationContext.xml&quot; }) with just @=
RunWith(MockitoJUnitRunner.class) at the top of the class. It works for me.=
</div><div><br></div><div>In Junits we normally do not make any external ca=
lls such as calls to DB or call to some other web service. We have to mock =
these external calls using @Mock annotations on this service objects. And t=
hen create a real object of the class that we are testing &amp; that depend=
s on these mocks. We can then use @InjectMocks on the real object so that i=
t will be injected with the mocked objects.</div><div><br></div><div>Exampl=
e Service-A-&gt;Calls-&gt;Service-B-&gt;Calls-&gt;Service-C</div><div><br><=
/div><div>While testing A we should mock Service B &amp; while testing Serv=
ice-B we should mock Service-C.</div><div><br></div><div>Some code snippet =
-</div><div><br></div><div>@RunWith(MockitoJUnitRunner.class)<br></div><div=
>public class TestServiceA {</div><div><br></div><div><span class=3D"" styl=
e=3D"white-space:pre">	</span>@Mock</div><div><span class=3D"" style=3D"whi=
te-space:pre">	</span>B mockObj;</div><div><br></div><div><span class=3D"" =
style=3D"white-space:pre">	</span>@InjectMocks</div><div><span class=3D"" s=
tyle=3D"white-space:pre">	</span>A realObj;</div><div>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 private String accId =
=3D &quot;a1234&quot;;</div><div><br></div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0@Test</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0public void testSe=
rviceA() {</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0Account acc =3D new Account(&quot;a1234&quot;, &quot;John&=
quot;, &quot;India&quot;);</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0when(mockObj.getAccDetails(anyString()).then=
Return(acc);</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0Account accountObj =3D realObj.getCustomerAccount(accId);<=
/div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0assertNotNull(accountObj);</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0}</div><div>} =C2=A0 =C2=A0 =C2=A0=C2=A0<br></div><div><br></div><div>So=
, I feel for Unit test cases we need not rely on Spring container to provid=
e us the instance of the class we are testing.</div><div><br></div><div>I m=
ight be wrong. Please give your suggestions.</div></div>




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

<br>


<br>

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


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

=20=20=20=20=20=20
=20=20

=20=20=20=20
    <div id=3D"fromDMARC" style=3D"clear:both; margin-top: 10px;">
         <hr style=3D"height:2px ; border-width:0; color:#E3E3E3; backgroun=
d-color:#E3E3E3;">
         Posted by: Ayaskant Swain &lt;[email protected]&gt;        =
 <hr style=3D"height:2px ; border-width:0; color:#E3E3E3; background-color:=
#E3E3E3;">
    </div>
<!-- Start Recommendations -->
<!-- End Recommendations -->



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

  <img src=3D"http://geo.yahoo.com/serv?s=3D97476590/grpId=3D2423328/grpspI=
d=3D1705006905/msgId=3D24674/stime=3D1435513991" width=3D"1" height=3D"1"> =
<br>

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

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

<br>



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

        <span id=3D"vithd" style=3D"font-weight: bold; color: #333; text-tr=
ansform: uppercase; "><a href=3D"https://groups.yahoo.com/neo/groups/junit/=
info;_ylc=3DX3oDMTJlbzJ2MzkyBF9TAzk3MzU5NzE0BGdycElkAzI0MjMzMjgEZ3Jwc3BJZAM=
xNzA1MDA2OTA1BHNlYwN2dGwEc2xrA3ZnaHAEc3RpbWUDMTQzNTUxMzk5MQ--" style=3D"tex=
t-decoration: none;">Visit Your Group</a></span>

     <ul style=3D"list-style-type: none; margin: 0; padding: 0; display: in=
line;">
            <li style=3D"border-right: 1px solid #000; font-weight: 700; di=
splay: inline; padding: 0 5px; margin-left: 0;">
      <span class=3D"cat"><a href=3D"https://groups.yahoo.com/neo/groups/ju=
nit/members/all;_ylc=3DX3oDMTJmOHM2NWRsBF9TAzk3MzU5NzE0BGdycElkAzI0MjMzMjgE=
Z3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwN2dGwEc2xrA3ZtYnJzBHN0aW1lAzE0MzU1MTM5OTE-" s=
tyle=3D"text-decoration: none;">New Members</a></span>
      <span class=3D"ct" style=3D"color: #ff7900;">2</span>
    </li>
                                              </ul>
  </div>


<div id=3D"ft" style=3D"font-family: Arial; font-size: 11px; margin-top: 5p=
x; padding: 0 2px 0 0; clear: both;">
  <a href=3D"https://groups.yahoo.com/neo;_ylc=3DX3oDMTJkajhxaHAyBF9TAzk3ND=
c2NTkwBGdycElkAzI0MjMzMjgEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwNmdHIEc2xrA2dmcARzd=
GltZQMxNDM1NTEzOTkx" style=3D"float: left;"><img src=3D"http://l.yimg.com/r=
u/static/images/yg/img/email/new_logo/logo-groups-137x15.png" height=3D"15"=
 width=3D"137" alt=3D"Yahoo! Groups" style=3D"border: 0;"/></a>
  <div style=3D"color: #747575; float: right;"> &bull; <a href=3D"https://i=
nfo.yahoo.com/privacy/us/yahoo/groups/details.html" style=3D"text-decoratio=
n: none;">Privacy</a> &bull; <a href=3D"mailto:junit-unsubscribe@yahoogroup=
s.com?subject=3DUnsubscribe" style=3D"text-decoration: none;">Unsubscribe</=
a> &bull; <a href=3D"https://info.yahoo.com/legal/us/yahoo/utos/terms/" sty=
le=3D"text-decoration: none;">Terms of Use</a> </div>
</div>

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

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

=20=20=20


  <br>

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


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

</html>

--001a113f9b7663da9e0519960a86--