0

I'm writing a small app and have encountered the following issue:

@Test
public void givenSkiExists_whenAddingNewSki_thenThrowEntityAlreadyPresentException () {
   skiManager.addEntity(new Ski(new SkiType("name1", "description1"), "brand1", "model1", "bond1", 1f));

   assertThrows(EntityAlreadyPresentException.class, () -> skiManager.addEntity(new Ski(new SkiType("name1", "description1"), "brand1", "model1", "bond1", 1f)));
}

the first skiManager.addEntity can throw multiple custom errors, but this test is not designed to check that, it assumes the first addEntity will work correctly, but I still need to handle the unhandled exceptions, and my question is:

Do I let it crash by just adding throws Exception to the unit test declaration or should I wrap it with try {...} catch (Exception e) {fail(e.message);}? is there any advantage to the second approach?

2
  • 1
    Definitely opinion-based. For my part I tend to be lazy and just let unexpected exceptions fall out of the test method. Often it gives simpler test code, which is an advantage. JUnit includes assertDoesNotThrow(). It seems to be included to take your situation into account. Commented Jun 20 at 10:27
  • 1
    What do you mean by “crash” here? A tear that fails it’s preconditions should fail, an exception outside of code under test will fail, tests aren’t actual code. Commented Jun 20 at 11:46

1 Answer 1

2

A test suite passes when all the tests in it pass or are skipped. If any test fails or errors out, the test suite does not pass.

With that in mind, there are very few practical differences between failing a test and allowing it to error out. Unless your catch block adds some useful behavior/info, I'd just let the test error out, and not bother with the boilerplate of catching all those exceptions in every test just to fail it.

For example:

catch (MyConfigException e) {
   fail("Got a MyConfigException. " + 
        "This probably means you forgot to set teh SOME_CONFIG env vairable", 
        e);
}

If multiple tests rely on the same set up logic, it might be a good idea to move it to some setup method, so if it errors out you get a clear indication where the problem was instead of having multiple tests failing for the same underlying reason.

For example:

public class SkiTests

private SkiManager skiManager;

@Before
public void setUp() {
    // Set up the SkiManager...

    // Set up the first entity
   skiManager.addEntity(new Ski(new SkiType("name1", "description1"), "brand1", "model1", "bond1", 1f));
}

@Test
public void givenSkiExists_whenAddingNewSki_thenThrowEntityAlreadyPresentException () {
    assertThrows(EntityAlreadyPresentException.class, () -> skiManager.addEntity(new Ski(new SkiType("name1", "description1"), "brand1", "model1", "bond1", 1f)));
}

@Test
public void testSomethingElseWithTheSameEntity() {
   //...
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.