Consider using @SafeVarargs for OngoingStubbing.thenThrow(Class<? extends Throwable>)
See original GitHub issueThe following code
Callable<Object> mock = mock(Callable.class);
Mockito.when(mock.call()).thenThrow(IOException.class);
causes an IMHO needless warning (using ECJ) on the second line:
Type safety: A generic array of Class<? extends Throwable> is created for a varargs parameter
AFAICT, thenThrow
’s implemention doesn’t warrant this warning. Hence, please consider using @SafeVarargs
here.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Why is varargs (Class<? extends Throwable>... t) "unchecked ...
Personally, I disable generic vararg warnings completely in my IDE, because they almost never indicate incorrect code from the point of view of ......
Read more >org.mockito.stubbing.OngoingStubbing.thenThrow java code ...
Sets a Throwable type to be thrown when the method is called. E.g: when(mock.someMethod()).thenThrow(RuntimeException.class);.
Read more >OngoingStubbing (Mockito 2.15.0 API) - Javadoc.io
Sets a Throwable type to be thrown when the method is called. OngoingStubbing<T>, thenThrow(Class<? extends Throwable> toBeThrown, Class<? extends ...
Read more >@SafeVarargs Annotation in Java 9 with Example ...
Read; Discuss; Practice; Video; Courses ... In JDK 9, JDK developers extended the use of @SafeVarargs annotation, now apart from the final ...
Read more >Uses of Interface org.mockito.stubbing.OngoingStubbing (Mockito ...
Sets consecutive return values to be returned when the method is called. OngoingStubbing<T>, OngoingStubbing. thenThrow(Class<? extends Throwable> throwableType).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Currently Mockito is built with Java8 (albeit we still compile with 1.6 runtime compatibility). Reading Rafael comment again (https://github.com/mockito/mockito/issues/372#issuecomment-227073725), can we now use the annotation while still be 1.6 compatible at runtime? If so, let’s make it happen (PR is welcome 😃.
Now that mockito 3 requires java 8 I took another look at this, but
@SafeVarags
can no be used, becauseOngoingStubbing
is an interface.By the way I do not get the warning reported (with Eclipse 4.5.2).