Junit Four Tutorial - Attempt Exception Thrown Past Times Coffee Method Amongst Example

One role of unit of measurement testing a Java method is checking exception thrown yesteryear that method. H5N1 Java unit of measurement essay should verify right exception thrown inwards particular instance together with no exception should live on thrown inwards normal case. In JUnit 3.XX, at that topographic point was no similar a shot back upward to move yesteryear or neglect a essay based upon exception thrown yesteryear a Java method. JUnit4 address this number together with provides an easy, together with readable way to test exception thrown yesteryear a Java method. There are many situations when you lot require to  test exception thrown yesteryear whatever method inwards Java. Classical instance of this is testing API methods which should throw IllegalArgumentException if arguments passed to method are non matching to pre-conditions. In fellowship to essay exception thrown yesteryear whatever method inwards JUnit iv you lot require to usage @Test(expected=IllegalArgumentException.class) annotation. You tin supplant IllegalArgumentException.class amongst whatever other exception e.g. NullPointerException.class or ArithmeticException.class etc.


Bottom trouble is that JUnit 4 will execute that essay 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 move yesteryear that test, but if method throws whatever other exception, or doesn't throw whatever exception than JUnit4 volition neglect that item test.



Unit essay to depository fiscal establishment check Excpetion thrown yesteryear Java method – JUnit iv Example

One role of unit of measurement testing a Java method is checking exception thrown yesteryear that method JUnit iv Tutorial - Test Exception thrown yesteryear Java Method amongst Example
In fellowship to essay whatever Java method for throwing excpetion inwards JUnit4,  You require to ensure that declaration provided to that method, from the essay must result inwards expected Exception, Other wise JUnit essay volition fail. Here is an instance of how to essay exception thrown inwards JUnit iv yesteryear 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 nada or negtaive it throws IllegalArgumentException. Let’s write a JUnit test to test this method for exception, only think that nosotros require to move yesteryear argument, which upshot inwards IllegalArgumentException.

here is the exmple of origin 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 instance of JUnit iv essay instance for Exception testing, you lot tin come across that our testSpeed() method is annotated amongst @Test(expected=IllegalArgumentException.class), which agency it await an illegalArgumentException, when you lot 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 upshot = instance.speed(distance, time); //shold throw exception
        assertEquals(expResult, result);     
    }
}

Test volition live on move yesteryear amongst electrical flow arguments, but if you lot alter arguments essay volition live on 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 essay exception inwards JUnit4. It’s extremely tardily to essay Java method amongst JUnit4’s notation based approach. Now you lot tin easily verify whatever Java method for both right together with wrong ready of inputs, along amongst exceptions for normal together with particular cases. If you lot are writing a populace API, which is intented to live on used yesteryear other, you lot must supply unit of measurement essay for checking exception thrown yesteryear a method, it’s imperative for whatever populace API author.

Further Learning
JUnit Best Practices for Java Programmer

Sumber https://javarevisited.blogspot.com/

0 Response to "Junit Four Tutorial - Attempt Exception Thrown Past Times Coffee Method Amongst Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel