2015-05-28 48 views
0

我跑在我的Eclipse下面的测试(带参数-ea):为什么我的Junit-AssertionError-Test失败?

public class ColorHelperTest 
{ 
    @Rule 
    public ExpectedException thrown = ExpectedException.none(); 

    @Test 
    public void testGetColorByString() 
    { 
     thrown.expect(AssertionError.class); 
     assert 1 == 2; 
    } 
} 

输出是:

java.lang.AssertionError 
at de.*.*.*.mytests.ColorHelperTest.testGetColorByString(ColorHelperTest.java:28) 

28是行assert 1==2

为什么这个测试失败?

回答

1

assert关键字是JRE级别的标志,与JUnit无关。我想你真正需要的是:

assertEquals(1, 2); 
+0

对不起,也许我是误导你,但断言只是一个例子,我不在乎。如果1!= 2 ;-) –

+0

我想找出如何捕获由其他方法抛出的AssertionErrors。 –

1

有趣的...你用什么版本的JUnit?在我的pom.xml使用4.12(看起来像:

<dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.12</version> 
     <scope>test</scope> 
    </dependency> 

完全相同的测试工作对我来说

+0

我使用Junit 4.11 –

+0

你设置了'-ea'标志吗?没有它,'assert'关键字被忽略。 –

+0

我做到了。如果我没有,我也不会得到AssertionError。 –