Junit循环处理预期的异常

另一个编译器错误

我已经学会了如何在Junit测试中捕获异常。但是现在我想循环遍历可能导致异常的参数,{null, Object.class}当前测试针对第一个循环运行,然后通过并存在,并且不检查下一个循环参数。

@Rule
public ExpectedException ee;

public ClassTest() {
    this.ee = ExpectedException.none();
}

/**
 * Test of isCompramised method.
 */
@Test
public void testIsCompramised2() {
    System.out.println("isCompramised Exception");
    Class<?>[] c = {null, Object.class};
    for (Class<?> class1 : c) {
        MyClass instance = new MyClass();

        ee.expect(IllegalArgumentException.class);
        boolean result = instance.isCompramised(class1);
        fail("Exception should have been thrown");

    }
}

所以我尝试了这一点,它完成了for循环,但是所有预期的异常都失败了,因为我认为Try Catch现在可以窃取该异常。

/**
 * Test of isCompramised method, of class MyClass.
 */
@Test
public void testIsCompramised2() {
    System.out.println("isCompramised Exception");
    Class<?>[] c = {null, Object.class};
    for (Class<?> class1 : c) {
        MyClass instance = new MyClass();
        try{
            ee.expect(IllegalArgumentException.class);
            boolean result = instance.isCompramised(class1);
            fail("Exception should have been thrown");
        } catch (Exception e){
            continue;
        }
    }
}

有什么建议吗?

这样对吗?

try{     
    boolean result = instance.isCompramised(class1);
    fail("Exception should have been thrown");
} catch (Exception e){
    AssertTrue(e instanceOf IllegalArgumentException);
    continue;
}
用户名

我会喜欢这样的东西:

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import java.util.Arrays;


@RunWith(Parameterized.class)
public class Foo {

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Parameter
    public Class<?> input;

    /**
     * Test of isCompramised method, of class MyClass.
     */
    @Test
    public void testIsCompramised() {
        this.expectedException.expect(IllegalArgumentException.class);
        final MyClass instance = new MyClass();
        instance.isCompramised(input);
    }

    @Parameters(name = "test for {0}")
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { {null}, {Object.class} });
    }
}

(或两种测试方法,一种用于null,一种用于Object


编辑:一些补充(请参阅注释)

带注解的方法@Parameters返回Iterable包含Object[]每个字段Object[]都绑定到一个带@Parameter注释的字段(使用@Parameteras的值作为索引[默认值:0])。JUnit运行器Parameterized将遍历@Parameters数据,并为每个数组设置字段值,然后运行该类中的每个测试。

另请参见:Parameterizedjavadoc

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JUnit预期异常

来自分类Dev

JUnit异常处理测试

来自分类Dev

JUnit异常处理测试

来自分类Dev

循环处理多个异常

来自分类Dev

while循环与异常处理

来自分类Dev

循环处理异常

来自分类Dev

JUnit如何处理预期的失败?

来自分类Dev

在while循环中处理异常

来自分类Dev

循环中的Python异常处理

来自分类Dev

在while循环中处理异常

来自分类Dev

循环中的Python异常处理

来自分类Dev

C++ 异常未按预期处理

来自分类Dev

异常处理 - 捕获多个未按预期工作的异常

来自分类Dev

如何使用jUnit测试已经处理的异常?

来自分类Dev

JUnit 异常处理不起作用

来自分类Dev

JUnit-预期的异常消息正则表达式

来自分类Dev

JUnit:测试异常无法正常工作(AssertionError:即使抛出异常也会出现预期的异常)

来自分类Dev

在异步循环中处理超时异常

来自分类Dev

如何忽略异常并继续处理foreach循环?

来自分类Dev

未处理的事件循环异常-OutOfMemoryError

来自分类Dev

C#异常处理,暂时停止循环

来自分类Dev

Junit测试预期的异常:java.io.FileNotFoundException,junit.framework.assertionFailedError

来自分类Dev

Junit测试预期的异常:java.io.FileNotFoundException,junit.framework.assertionFailedError

来自分类Dev

Windows批处理脚本:ENABLEDELAYEDEXPANSION + For循环无法按预期工作

来自分类Dev

处理对象数组时,For循环无法按预期工作

来自分类Dev

如何在不退出循环的情况下处理异常?

来自分类Dev

异常处理,捕获导致while循环停止的原因

来自分类Dev

使用循环进行输入验证和异常处理

来自分类Dev

处理异常时奇怪的嵌套循环行为