n2o / url-shortener

Shorty: The URL Shortener
MIT License
17 stars 15 forks source link

Exception throw on test #24

Closed Segelzwerg closed 4 years ago

Segelzwerg commented 4 years ago

I have a idea for code quality. I found this lombok annotation @SneakyThrows. From lombok documentation:

@SneakyThrows can be used to sneakily throw checked exceptions without actually declaring this in your method's throws clause.

We could use this on our controller tests EDIT:

@SneakyThrows
@Test
void testIndex() {

instead of

https://github.com/n2o/url-shortener/blob/a90ac854c324b5521b6e927bf0964d66b2d94cd7/src/test/java/de/hhu/propra/link/controllers/LinkControllerTest.java#L29-L34

what do you think @n2o @iTitus

n2o commented 4 years ago

Shouldn't it transform to this?

@SneakyThrows
@Test
void testIndex() {
    //
}

I think we can use it, but it is not really better than the throws clause 🤔

Segelzwerg commented 4 years ago

yes, was a bad copy and paste. I think the advantage is you acknowledge it is bit ugly and you didn't add the throw by mistake

iTitus commented 4 years ago

This is a bad use case for @SneakyThrows.

The intended use cases are listed here.

They boil down to this: You have to do exception handling in a function because something is throwing an unchecked exception but...

...and you don't want to deal with unnecessary syntax (try-catch with rethrow or swallow).

iTitus commented 4 years ago

We have full control over the test methods and the exceptions that can arise are all valid and possible. So it is enough to just declare the methods as throws T. @SneakyThrows gives no extra value.

Segelzwerg commented 4 years ago

I see, thanks for clearing that up @iTitus. Maybe I spent to much time annotating stuff :wink: