在Kotlin中测试预期的异常 [英] Test expected exceptions in Kotlin

查看:86
本文介绍了在Kotlin中测试预期的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,程序员可以为JUnit测试用例指定预期的异常,如下所示:

In Java, the programmer can specify expected exceptions for JUnit test cases like this:

@Test(expected = ArithmeticException.class)
public void omg()
{
    int blackHole = 1 / 0;
}

我将如何在Kotlin中做到这一点?我尝试了两种语法变体,但没有一个起作用:

How would I do this in Kotlin? I have tried two syntax variations, but none of them worked:

import org.junit.Test as test

// ...

test(expected = ArithmeticException) fun omg()
    Please specify constructor invocation;
    classifier 'ArithmeticException' does not have a companion object

test(expected = ArithmeticException.class) fun omg()
                           name expected ^
                                           ^ expected ')'

推荐答案

语法很简单:

@Test(expected = ArithmeticException::class)

这篇关于在Kotlin中测试预期的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆