Maven test and excepted test annotation

Viewed 140

I'm using Maven, and I try to make test using Junit.

When I want to declare a test which check if an exception is thrown, currently I do this :

@Test
public void testAddPlayerWithInvalidBirthdate() throws Exception {
    boolean test = false;

    try {
        this.playerService.add("Jean", "Dupont", new MyCalendar(2010, 12, 21), "test", "test");
    } catch (MinorPersonException e) {
        test = true;
    }

    assertTrue(test);
}

This test works good But when I try to write this :

@Test(expected = MinorPersonException.class)
public void testAddPlayerWithInvalidBirthdate() throws Exception {
    this.playerService.add("Jean", "Dupont", new MyCalendar(2010, 12, 21), "test", "test");
}

And when I process the tests with mvn test, maven says the Exception has been thrown and the test has failed :/

I'm using JUnit 4.12 in my pom.xml

EDIT : Example

My test class :

public class PlayerParametersTest extends TestCase {
    @Test(expected = InvalidNameException.class)
    public void testInvalidFirstName() throws Exception {
        new Player("", "Dupont", new MyCalendar(1980, 03, 17), "test", "test");
    }
}

Maven result :

Results :

Tests in error: 
  PlayerParametersTest.testInvalidFirstName:14 » InvalidName Invalid firstname 
1 Answers
Related