如何在Eclipse中获得有关JUnit测试的反馈

时间:2012-01-30 19:57:56

标签: eclipse testing junit testcase

我在Eclipse中有一个简单的JUnit测试。

public class HelloTest extends TestCase {
    CalculatorEngine ce;

    public HelloTest(String name) {
        super(name);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        ce = new CalculatorEngine();
    }

    @Override
    protected void tearDown() throws Exception {
        // TODO Auto-generated method stub
        super.tearDown();
        ce = null;
    }

    public void test1() {
        assertTrue(ce.doCalculation("1+5").equals("2"));
    }
}

测试失败,因为1 + 5不等于2.如果我将1 + 5更改为1 + 1,则测试成功。

如何从JUnit获取一些反馈/输出以确定测试失败时的结果?换句话说,有什么方法我可以发现ce.doCalculation(“1 + 5”)返回6而不是2?

2 个答案:

答案 0 :(得分:3)

您可以使用assertEquals方法进行检查

assertEquals("Unexpected result!", 2, ce.doCalculation("1+5"));

(可用于检查/比较大多数类型 - 查看API文档)。

答案 1 :(得分:1)

您还可以使用assertThat api编写更多描述性测试,例如: -

assertThat(1, is(2))

assertThat(1, is(not(2))