JUnit 预期标记未按预期工作 [英] JUnit expected tag not working as expected

查看:25
本文介绍了JUnit 预期标记未按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Eclipse 中有以下测试用例,使用拒绝通过的 JUnit 4.有什么问题?

I have the following test case in eclipse, using JUnit 4 which is refusing to pass. What could be wrong?

@Test(expected = IllegalArgumentException.class)
public void testIAE() {
    throw new IllegalArgumentException();
}

这个确切的测试用例是在尝试使用预期标签测试我自己的代码不起作用时出现的.我想看看 JUnit 是否会通过最基本的测试.没有.

This exact testcase came about when trying to test my own code with the expected tag didn't work. I wanted to see if JUnit would pass the most basic test. It didn't.

我也按预期测试了自定义异常,但没有运气.

I've also tested with custom exceptions as expected without luck.

屏幕截图:

推荐答案

问题是你的AnnounceThreadTest 扩展了TestCase.因为它扩展了 TestCase,JUnit Runner 将其视为 JUnit 3.8 测试,并且该测试正在运行,因为它以单词 test 开头,隐藏了事实上根本没有使用 @Test 注解的事实.

The problem is that your AnnounceThreadTest extends TestCase. Because it extends TestCase, the JUnit Runner is treating it as a JUnit 3.8 test, and the test is running because it starts with the word test, hiding the fact that the @Test annotiation is in fact not being used at all.

要解决此问题,请从类定义中删除extends TestCase".

To fix this, remove the "extends TestCase" from the class definition.

这篇关于JUnit 预期标记未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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