| Newsgroups |
gmane.comp.java.junit.user |
| Message-ID |
<CADW0UyT2MGvjTEXQKN9VMvqJmDRhenAhXJMGFcHgokWNfxaSUw@mail.gmail.com> |
--94eb2c129c24371ba80558168dff
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Short version: use assertThrows().
I would recommend against using the 'expected' parameter of @Test. It only
allows you to assert that there exists some code in the test method that
throws an exception of the given type. The test can pass for the wrong
reason. Also, there's no way to make assertions on the exception message or
exception cause when you use @Test(expected =3D MyException.class).
I would also recommend against ExpectedException. I've seen to many cases
where it was used incorrectly (either doing validation after the method
that is expected to throw, or using ExpectedException inside of helper
method).
In JUnit 4.13 and JUnit 5, you will be able to use assertThrows() instead,
which allows the test to isolate what part of the test method is expected
to throw:
@Test
public void popWithEmptyStackShouldThrow() {
Stack stack =3D new Stack();
assertThrows(EmptyStackException.class, () -> stack.pop());
}
Note that the JUnit 5 versions of assertThrows() allows you to specify a
message or a supplier for a message. We didn't add a way to specify a
message in JUnit 4.13; we could add it, but I've seen some large teams use
assertThrows() and no one has asked for a way to customize the exception
message.
For JUnit 4.12 and below, I recommend good 'ol try...catch
The only safe use cases I see for the 'expected' parameter of @Test are
when the only thing the test method does is call a single method that is
expected to throw. In that case, you can use 'expected' and just give the
test method a readable name:
private final MyObject myObject =3D new MyObject();
@Test(expected =3D NullPointerException.class)
public void doSomething_paramMustNotBeNull() {
myObject.doSomething(null);
}
That being said, once you get used to lambdas, the assertThrows() version
is very readable:
@Test
public void doSomething_paramMustNotBeNull() {
assertThrows(
NullPointerException.class, () -> myObject.doSomething(null));
}
-- Kevin
On Thu, Aug 31, 2017 at 10:13 AM, [email protected] [junit] <
[email protected]> wrote:
>
>
> Would it make sense to enhance the @Test annotation with a new optional
> message element? Currently all of the Assert methods have overloaded form=
s
> including a message parameter in order to report what went wrong in human
> readable terms. However when you use the @Test annotation with an expecte=
d
> element to check that a particular exception was thrown there's no way to
> report a custom message back to the user.
>
> http://junit.org/junit4/javadoc/latest/org/junit/Test.html#expected()
>
> Alternatively, could we add a method to the ExpectedException rule to set
> a custom failure message?
>
> http://junit.org/junit4/javadoc/latest/org/junit/
> rules/ExpectedException.html
>=20
>
--94eb2c129c24371ba80558168dff
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>Short version: use assertThrows().</div><div><br></di=
v><div>I would recommend against using the 'expected' parameter of =
@Test. It only allows you to assert that there exists some code in the test=
method that throws an exception of the given type. The test can pass for t=
he wrong reason. Also, there's no way to make assertions on the excepti=
on message or exception cause when you use @Test(expected =3D MyException.c=
lass).<br></div><div><br></div><div>I would also recommend against Expected=
Exception. I've seen to many cases where it was used incorrectly (eithe=
r doing validation after the method that is expected to throw, or using Exp=
ectedException inside of helper method).</div><div><br><div>In JUnit 4.13 a=
nd JUnit 5, you will be able to use assertThrows() instead, which allows th=
e test to isolate what part of the test method is expected to throw:</div><=
div><br></div><div><div><font face=3D"monospace, monospace">@Test<br>public=
void popWithEmptyStackShouldThrow() {</font></div><div><font face=3D"monos=
pace, monospace">=C2=A0 Stack stack =3D new Stack();</font></div><div><font=
face=3D"monospace, monospace">=C2=A0 assertThrows(EmptyStackException.clas=
s, () -> stack.pop());</font></div><div><font face=3D"monospace, monospa=
ce">}</font></div></div><div><br></div><div>Note that the JUnit 5 versions =
of assertThrows() allows you to specify a message or a supplier for a messa=
ge. We didn't add a way to specify a message in JUnit 4.13; we could ad=
d it, but I've seen some large teams use assertThrows() and no one has =
asked for a way to customize the exception message.</div><div><br></div><di=
v>For JUnit 4.12 and below, I recommend good 'ol try...catch</div><div>=
<div><br></div><div>The only safe use cases I see for the 'expected'=
; parameter of @Test are when the only thing the test method does is call a=
single method that is expected to throw. In that case, you can use 'ex=
pected' and just give the test method a readable name:</div><div><br></=
div><div><font face=3D"monospace, monospace">private final MyObject myObjec=
t =3D new MyObject();</font></div><div><font face=3D"monospace, monospace">=
<br></font></div><div><font face=3D"monospace, monospace">@Test(expected =
=3D NullPointerException.class)<br>public void doSomething_paramMustNotBeNu=
ll() {</font></div><div><font face=3D"monospace, monospace">=C2=A0 myObject=
.doSomething(null);</font></div><div><font face=3D"monospace, monospace">}<=
/font></div><div><br></div><div>That being said, once you get used to lambd=
as, the assertThrows() version is very readable:</div><div><br></div><div><=
div><font face=3D"monospace, monospace">@Test<br>public void doSomething_pa=
ramMustNotBeNull() {</font></div><div><font face=3D"monospace, monospace">=
=C2=A0=C2=A0assertThrows(</font></div><div><font face=3D"monospace, monospa=
ce">=C2=A0 =C2=A0 =C2=A0 NullPointerException.class, () ->=C2=A0myObject=
.doSomething(null));</font></div><div><font face=3D"monospace, monospace">}=
</font></div></div><div><div><div class=3D"gmail_extra"><div><div class=3D"=
gmail_signature"><br>-- Kevin<br></div></div>
<br><div class=3D"gmail_quote">On Thu, Aug 31, 2017 at 10:13 AM, <a href=3D=
"mailto:[email protected]">[email protected]</a> [junit] <span dir=3D=
"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank">junit@=
yahoogroups.com</a>></span> wrote:<br><blockquote class=3D"gmail_quote" =
style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pa=
dding-left:1ex">
<u></u>
=20
<div style=3D"background-color:rgb(255,255,255)">
<span style=3D"display:none">=C2=A0</span>
<div id=3D"gmail-m_-4319757940206689135ygrp-mlmsg">
<div id=3D"gmail-m_-4319757940206689135ygrp-msg">
<div id=3D"gmail-m_-4319757940206689135ygrp-text">
=20=20=20=20=20=20
=20=20=20=20=20=20
<p>Would it make sense to enhance the @Test annotation with a new opt=
ional message element? Currently all of the Assert methods have overloaded =
forms including a message parameter in order to report what went wrong in h=
uman readable terms. However when you use the @Test annotation with an expe=
cted element to check that a particular exception was thrown there's no=
way to report a custom message back to the user.<br>
<br>
<a href=3D"http://junit.org/junit4/javadoc/latest/org/junit/Test.html#expec=
ted()" target=3D"_blank">http://junit.org/junit4/<wbr>javadoc/latest/org/ju=
nit/Test.<wbr>html#expected()</a><br>
<br>
Alternatively, could we add a method to the ExpectedException rule to set a=
custom failure message?<br>
<br>
<a href=3D"http://junit.org/junit4/javadoc/latest/org/junit/rules/ExpectedE=
xception.html" target=3D"_blank">http://junit.org/junit4/<wbr>javadoc/lates=
t/org/junit/<wbr>rules/ExpectedException.html</a></p>
</div>
=20=20=20=20=20
=20=20=20=20
<div style=3D"color:rgb(255,255,255);height:0px"></div>
</div>
=20=20
</blockquote></div><br></div></div></div></div></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: =3D?UTF-8?Q?Kevin_Cooney_=3DE2=3D98=3D95?=3D <kcoone=
[email protected]> <hr style=3D"height:2px ; border-width:0; color:#E3=
E3E3; 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=3D24772/stime=3D1504280745" 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=3DX3oDMTJlb25zOTFsBF9TAzk3MzU5NzE0BGdycElkAzI0MjMzMjgEZ3Jwc3BJZAM=
xNzA1MDA2OTA1BHNlYwN2dGwEc2xrA3ZnaHAEc3RpbWUDMTUwNDI4MDc0NQ--" 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=3DX3oDMTJmc29wMmhpBF9TAzk3MzU5NzE0BGdycElkAzI0MjMzMjgE=
Z3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwN2dGwEc2xrA3ZtYnJzBHN0aW1lAzE1MDQyODA3NDU-" s=
tyle=3D"text-decoration: none;">New Members</a></span>
<span class=3D"ct" style=3D"color: #ff7900;">1</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=3DX3oDMTJkcWc0dGxzBF9TAzk3ND=
c2NTkwBGdycElkAzI0MjMzMjgEZ3Jwc3BJZAMxNzA1MDA2OTA1BHNlYwNmdHIEc2xrA2dmcARzd=
GltZQMxNTA0MjgwNzQ1" 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;"> • <a href=3D"https://i=
nfo.yahoo.com/privacy/us/yahoo/groups/details.html" style=3D"text-decoratio=
n: none;">Privacy</a> • <a href=3D"mailto:junit-unsubscribe@yahoogroup=
s.com?subject=3DUnsubscribe" style=3D"text-decoration: none;">Unsubscribe</=
a> • <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>
--94eb2c129c24371ba80558168dff--