Making JUnit ignore test methods

If we want JUnit to ignore certain test methods we can do that by using the @Ignore annotation applied on the methods that has to be ignored. The below test class tests a functionality of a calculator

CalculatorTest


package Package1;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class CalculatorTest {

    @Test
    public void testAdd() {
        int result = Calculator.add(5, 6);
        assertEquals(result, 11);
    }

    @Test
    public void testSubstract() {
        int result = Calculator.substract(6, 5);
        assertEquals(result,1);
    }

    @Test
    public void testMultiply() {
        int result = Calculator.multiply(6, 5);
        assertEquals(result, 30);
    }

    @Ignore
    @Test
    public void testDivide() {
        int result = Calculator.divide(6, 5);
        assertEquals(result, 1);
    }
}

Calculator


package Package1;

public class Calculator {
    public static int add(int op1, int op2) {
        return op1 + op2;
    }

    public static int substract(int op1, int op2) {
        return op1 - op2;
    }

    public static int multiply(int op1, int op2) {
        return op1 * op2;
    }

    public static int divide(int op1, int op2) {
        return op1/op2;
    }
}

As shown in the above code, @Ignore is applied testDivide method. As a result JUnit will not execute the method.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s