Reputation: 1256
I run the following test in my Eclipse (with arguments -ea):
public class ColorHelperTest
{
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testGetColorByString()
{
thrown.expect(AssertionError.class);
assert 1 == 2;
}
}
The output is:
java.lang.AssertionError
at de.*.*.*.mytests.ColorHelperTest.testGetColorByString(ColorHelperTest.java:28)
28 is the line assert 1==2
Why does this test fail?
Upvotes: 0
Views: 533
Reputation: 7810
You cannot "expect" AssertionError
, this is the specific type of error that JUnit uses to signal that the test is failing.
Update: Turns out it's a bug of JUnit 4.11, and it was resolved in 4.12: https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.12.md#pull-request-583-pull-request-720-fix-handling-of-assertionerror-and-assumptionviolatedexception-in-expectedexception-rule
Upvotes: 2
Reputation: 370
Interesting... What version of Junit you use? Using 4.12 (in my pom.xml looks like:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
the exact same test works for me.
Upvotes: 1
Reputation: 21184
The assert
keyword is a JRE level flag and has nothing to do with JUnit. I think what you're really looking for is:
assertEquals(1, 2);
Upvotes: 1