1

Junit 4参数化测试中,如果我在测试类中有 3 个测试并且我想为特定测试使用不同的参数,我应该怎么做?

可以说 may 3rd test 检查是否抛出了特定的异常。所以我只需要将错误的参数传递给那个测试。

@RunWith(Parameterized.class)
public class EvenNumberProcessorTest {
    private int num1;
    private int num2;

    public int EvenNumberProcessorTest(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    @Parameterized.Parameters
    public static Collection<Integer[]> numbers() {
        return Arrays.asList(new Integer[][] {
            {4, 2},
            {5, 2}
        });
    }

    @Test
    public void testCheckDivision() {
        Assert.asserEquals(true, EvenNumberProcessor.checkDivision(num1, num2));
    }

    @Test(expected = MyException.class)
    public void testCheckDivisionFail() {
        DivisionProcessor.checkDivision(3, 0);
    }

}

public boolean checkDivision(int num1, num2) {
    boolean result = false;

    try {
        if (num1 % num2 == 0) {
            result = true;
        } else {
            result = false;
        }
    } catch (ArithmeticException e) {
        throw new MyException("You can not use 0 to divide");
    }

    return result;
}
4

2 回答 2

0

@Parameterized这个有点太重了。尝试一些更简单的东西。下面的示例使用zohhakassertj

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;

@RunWith(ZohhakRunner.class)
public class MyTest {

    @Test(expected=IllegalArgumentException.class)
    public void should_throw_exception_on_division_by_zero() {
        checkDivision(3, 0);
    }

    @TestWith({
        "4, 2, true",
        "5, 2, false"
    })
    public void should_check_if_divisable(int num1, int num2, boolean isDivisible) {
        assertThat(checkDivision(num1, num2)).isEqualTo(isDivisible);
    }

    public boolean checkDivision(int num1, int num2) {
        ...
    }
}
于 2014-12-27T22:05:28.850 回答
0

您可以通过添加另一个参数在测试的构造函数中传递预期的输出。我已更新您的代码以反映以下内容:

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class EvenNumberProcessorTest  {
    private final int num1;
    private final int num2;
    private final boolean expectFailure;

    public EvenNumberProcessorTest(int num1, int num2, boolean expectFailure) {
        this.num1 = num1;
        this.num2 = num2;
        this.expectFailure = expectFailure;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> numbers() {
        return Arrays.asList(new Object[][] { { 4, 2, false }, { 5, 0, true } });
    }

    @Test
    public void testCheckDivision() {
        try {
            boolean result = EvenNumberProcessor.checkDivision(num1, num2);
            if (expectFailure) {
                junit.framework.Assert.fail("MyException expected but not thrown.");
            } else {
                junit.framework.Assert.assertEquals(true, result);
            }
        } catch (Exception ex) {
            if (!expectFailure) {
                junit.framework.Assert.fail("MyException not expected but thrown.");
            }
        }
    }

}

正如您可能已经猜到的那样,您可以将预期结果(真/假)传递给构造函数。

于 2014-05-30T10:57:54.120 回答