程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法测试返回customException的类大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法测试返回customException的类?

开发过程中遇到无法测试返回customException的类的问题如何解决?下面主要结合日常开发的经验,给出你关于无法测试返回customException的类的解决方法建议,希望对你解决无法测试返回customException的类有所启发或帮助;

我在上面的评论中同意@stultuske,并将测试重写为:@H_616_3@

@Test
public voID shouldThrowExceptionForInvalIDString() {

    try {
        MyClass myCls = new MyClass();
        Method valStr = myCls.getClass().getDeclaredMethod(
                "valIDateString", String.class);
        valStr.setAccessible(true);
        valStr.invoke(myCls, "This is theDummyWord find it if you can.");
    } catch (Exception E) {
        assert(e instanceOf CustomException);
        assert(e.getmessage.equals("String has the invalID word!"));
    }

}

或者如果您想使用ExpectedException@H_616_3@

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

@Test
public voID shouldThrowExceptionForInvalIDString() {

    thrown.expect(CustomException.class);
    thrown.expectmessage("String has the invalID word!");

    MyClass myCls = new MyClass();
    Method valStr = myCls.getClass().getDeclaredMethod("valIDateString", String.class);
    valStr.setAccessible(true);
    valStr.invoke(myCls, "This is theDummyWord find it if you can.");

}

解决方法

在尝试JUnit时,我尝试如下测试一个简单的私有方法,该方法接收String并确保其中不包含单词’Dummy’。@H_616_3@

我知道,可以将测试 与类 放在同一包 中,并将方法的访问修饰符更改为包,但是我想使用反射来学习这一点。@H_616_3@

private void validateString(String myString) throws CustomException {

    if (myString.toLowerCase().matches(".*dummy.*"))
        throw new CustomException("String has the invalid word!");

}

我试图通过反射访问私有方法,但测试失败!它显示以下异常:@H_616_3@

java.lang.AssertionError:Expected test to throw
(an instance of com.myproject.exception.CustomException and exception 
with message a String containing "String has the invalid word!")

基于这个问题的答案,我也正在抓InvocationTargetException。@H_616_3@

JUnit的@H_616_3@

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

    @Test
    public void shouldThrowExceptionForInvalidString() {

        thrown.expect(CustomException.class);
        thrown.expectmessage("String has the invalid word!");

        try {
            MyClass myCls = new MyClass();
            Method valStr = myCls.getClass().getDeclaredMethod(
                    "validateString",String.class);
            valStr.setAccessible(true);
            valStr.invoke(myCls,"This is theDummyWord find it if you can.");
        } catch (InvocationTargetException | NoSuchMethodException
                | SecurityException | IllegalAccessException
                | IllegalArgumentexception n) {
            if (n.getCause().getClass() == CustomException.class) {
                throw new CustomException("String has the invalid word!");
            }
        }

    }

大佬总结

以上是大佬教程为你收集整理的无法测试返回customException的类全部内容,希望文章能够帮你解决无法测试返回customException的类所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。