I use the JUnit 4 annotations as well.  The specific problem is as coded,
this test will pass when any of the lines throw the expected exception.  You
want the final line to throw the exception, but if your code breaks and the
remove method starts throwing this exception, this test will pass.  In that
scenario, you'd want this test to fail.

Current code:


@Test
@ExpectedException(DataAccessException.class)
public void testAddAndRemovePerson() throws Exception {
    Person person = new Person();
    person.setFirstName("Country");
    person.setLastName("Bry");

    person = personDao.save(person);
    flush();

    person = personDao.get(person.getId());

    assertEquals("Country", person.getFirstName());
    assertNotNull(person.getId());

    log.debug("removing person...");

    personDao.remove(person.getId());
    flush();

    // should throw DataAccessException
    personDao.get(person.getId());
}


Suggestion:


@Test
@ExpectedException(DataAccessException.class)
public void testAddAndRemovePerson() throws Exception {
    Person person = new Person();
    person.setFirstName("Country");
    person.setLastName("Bry");

    try {

    person = personDao.save(person);
    flush();

    person = personDao.get(person.getId());

    assertEquals("Country", person.getFirstName());
    assertNotNull(person.getId());

    log.debug("removing person...");

    personDao.remove(person.getId());
    flush();

    } catch (DataAccessException e) {
        fail( ... );
    }

    // should throw DataAccessException
    personDao.get(person.getId());
}


Now, the test generates the expected exception in only the 1 place where you
want it to.  If other methods start throwing the exception, the test would
now fail.



-- 
View this message in context: 
http://n4.nabble.com/Integration-Tests-tp1563151p1569446.html
Sent from the AppFuse - User mailing list archive at Nabble.com.

Reply via email to