Junit Iv Tutorial - Examine Exception Thrown Past Times Coffee Method Amongst Example
One purpose of unit of measurement testing a Java method is checking exception thrown past times that method. H5N1 Java unit of measurement attempt out should verify right exception thrown inward particular illustration together with no exception should endure thrown inward normal case. In JUnit 3.XX, at that spot was no forthwith back upwards to top or neglect a attempt out based upon exception thrown past times a Java method. JUnit4 address this number together with provides an easy, together with readable way to test exception thrown past times a Java method. There are many situations when yous bespeak to test exception thrown past times whatever method inward Java. Classical illustration of this is testing API methods which should throw IllegalArgumentException if arguments passed to method are non matching to pre-conditions. In guild to attempt out exception thrown past times whatever method inward JUnit iv yous bespeak to utilization @Test(expected=IllegalArgumentException.class) annotation. You tin supersede IllegalArgumentException.class amongst whatever other exception e.g. NullPointerException.class or ArithmeticException.class etc.
Bottom draw of piece of job is that JUnit 4 will execute that attempt out together with depository fiscal establishment check if method genuinely throws expected exception or not. If method throws expected exception, specified equally "expected", than JUnit iv volition top that test, but if method throws whatever other exception, or doesn't throw whatever exception than JUnit4 volition neglect that item test.
Bottom draw of piece of job is that JUnit 4 will execute that attempt out together with depository fiscal establishment check if method genuinely throws expected exception or not. If method throws expected exception, specified equally "expected", than JUnit iv volition top that test, but if method throws whatever other exception, or doesn't throw whatever exception than JUnit4 volition neglect that item test.
Unit attempt out to depository fiscal establishment check Excpetion thrown past times Java method – JUnit iv Example
In guild to attempt out whatever Java method for throwing excpetion inward JUnit4, You bespeak to ensure that declaration provided to that method, from the attempt out must result inward expected Exception, Other wise JUnit attempt out volition fail. Here is an illustration of how to attempt out exception thrown inward JUnit iv past times testing a method called speed(), which returns speed equally distance/time, but earlier calculating speed it checkes wheter fourth dimension together with distance is positive or not, together with if fourth dimension is aught or negtaive it throws IllegalArgumentException. Let’s write a JUnit test to test this method for exception, merely think that nosotros bespeak to top argument, which lawsuit inward IllegalArgumentException.
here is the exmple of root class:
public class SpeedUtils {
public int speed (int distance, int time){
if(distance < 0 || fourth dimension <= 0){
throw new IllegalArgumentException("distance: " + distance
+ " time: " + time);
}
return distance/time;
}
}
and hither is the illustration of JUnit iv attempt out illustration for Exception testing, yous tin meet that our testSpeed() method is annotated amongst @Test(expected=IllegalArgumentException.class), which agency it hold off an illegalArgumentException, when yous run this JUnit test.
public class JUnitExceptionTest {
/**
* Test of speed method, of shape JUnit4ExceptionTest.
*/
@Test(expected=IllegalArgumentException.class)
public void testSpeed() {
System.out.println("speed");
int distance = 0;
int fourth dimension = 0;
JUnit4ExceptionTest instance = new JUnit4ExceptionTest();
int expResult = 0;
int lawsuit = instance.speed(distance, time); //shold throw exception
assertEquals(expResult, result);
}
}
Test volition endure top amongst electrical flow arguments, but if yous alter arguments attempt out volition endure failed similar below, because it won’t larn IllegalArgumentException whatever more.
Testcase: testSpeed(test.JUnit4ExceptionTest): FAILED
Expected exception: java.lang.IllegalArgumentException
junit.framework.AssertionFailedError: Expected exception: java.lang.IllegalArgumentExcepti
That's all on how to attempt out exception inward JUnit4. It’s extremely slow to attempt out Java method amongst JUnit4’s tone based approach. Now yous tin easily verify whatever Java method for both right together with wrong fix of inputs, along amongst exceptions for normal together with particular cases. If yous are writing a world API, which is intented to endure used past times other, yous must supply unit of measurement attempt out for checking exception thrown past times a method, it’s imperative for whatever world API author.
Further Learning
JUnit Best Practices for Java Programmer

Komentar
Posting Komentar