java - IllegalArgumentException.class as expected in JUnit -
let's have simple method want unit test:
public class add { public static int addthefollowingpositivenumbertoone(int a) { if(a <= 0) { throw new illegalargumentexception(); } int b = 1 + a; return b; } }
i want make sure illegalargumentexception thrown if parameter less or equal 0.
the junit code this:
@test (expected = illegalargumentexception.class) public void testillegalargumentexception() { int = -1; add.addthefollowingpositivenumbertoone(a); }
why have add .class @ end?
if see api document of expected() parameter, takes parameter of type class
. that's why passing illegalargumentexception
class type illegalargumentexception.class
.
Comments
Post a Comment