Consider the following test case:
@Test
public void testFooBar ()
throws FooBadParameterException,
FooInvalidStateException,
BarUnmatchedException,
BarDuplicatedException
{
Foo foo = fooService.generateFoo();
Bar bar = barService.generateBar();
assertThat(foo.getId()).isEqualTo(bar.getId());
}
where FooBadParameterException
, FooInvalidStateException
, BarUnmatchedException
, BarDuplicatedException
are the checked exceptions that may be thrown by fooService.generateFoo()
and barService.generateBar()
.
My question is: can I simplify the above test case into:
@Test
public void testFooBar ()
throws Exception
{
Foo foo = fooService.generateFoo();
Bar bar = barService.generateBar();
assertThat(foo.getId()).isEqualTo(bar.getId());
}
Although I didn't think of any reason not to do this, I want to ask if there is any hidden downside to do so.
Note: in In Java, is using throws Exception instead of throwing multiple specific exceptions good practice?, the accepted answer only cover the case for Spring MVC. My question focuses on test cases.