question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider using @SafeVarargs for OngoingStubbing.thenThrow(Class<? extends Throwable>)

See original GitHub issue

The 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:open
  • Created 8 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mockitoguycommented, Jan 4, 2018

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 😃.

1reaction
PascalSchumachercommented, Sep 25, 2016

Now that mockito 3 requires java 8 I took another look at this, but @SafeVarags can no be used, because OngoingStubbing is an interface.

By the way I do not get the warning reported (with Eclipse 4.5.2).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found