Quantcast
Channel: Using Mockito to mock classes with generic parameters - Stack Overflow
Browsing latest articles
Browse All 13 View Live

Answer by MevlütÖzdemir for Using Mockito to mock classes with generic...

The (in my opinion) most easiest and most readable approach is to use method level injection.This will result in having all test data within the test method. This will keep your test classes clean as...

View Article


Answer by Prasannjeet Singh for Using Mockito to mock classes with generic...

So you have this:Foo mockFoo = mock(Foo.class);Ways to fix it, starting from my least favourite to most:Use @SuppressWarnings("unchecked") annotation. Doesn't really fix it, but you'll stop getting the...

View Article

Answer by 范仲毅 for Using Mockito to mock classes with generic parameters

why not using spyvar mock = spy(new Foo<Bar>());when(mockFoo.getValue()).thenReturn(new Bar());

View Article

Answer by xilef for Using Mockito to mock classes with generic parameters

JUnit5: use @ExtendWith(MockitoExtension.class) on the test class then add this field:@MockFoo<Bar> barMock;

View Article

Answer by vogella for Using Mockito to mock classes with generic parameters

With JUnit5 I think the best way is to @ExtendWith(MockitoExtension.class) with @Mock in the method parameter or the field.The following example demonstrates that with Hamcrest matchers.package...

View Article


Answer by M. Justin for Using Mockito to mock classes with generic parameters

As the other answers mentioned, there's not a great way to use the mock()& spy() methods directly without unsafe generics access and/or suppressing generics warnings.There is currently an open...

View Article

Answer by Tobias Uhmann for Using Mockito to mock classes with generic...

I agree that one shouldn't suppress warnings in classes or methods as one could overlook other, accidentally suppressed warnings. But IMHO it's absolutely reasonable to suppress a warning that affects...

View Article

Answer by acdcjunior for Using Mockito to mock classes with generic parameters

Create a test utility method. Specially useful if you need it for more than once.@Testpublic void testMyTest() { // ... Foo<Bar> mockFooBar = mockFoo(); when(mockFooBar.getValue).thenReturn(new...

View Article


Answer by qza for Using Mockito to mock classes with generic parameters

Here is an interesting case: method receieves generic collection and returns generic collection of same base type. For example:Collection<? extends Assertion> map(Collection<? extends...

View Article


Answer by dsingleton for Using Mockito to mock classes with generic parameters

You could always create an intermediate class/interface that would satisfy the generic type that you are wanting to specify. For example, if Foo was an interface, you could create the following...

View Article

Answer by Marek Kirejczyk for Using Mockito to mock classes with generic...

One other way around this is to use @Mock annotation instead.Doesn't work in all cases, but looks much sexier :)Here's an example:@RunWith(MockitoJUnitRunner.class)public class FooTests { @Mock public...

View Article

Answer by John Paulett for Using Mockito to mock classes with generic parameters

I think you do need to cast it, but it shouldn't be too bad:Foo<Bar> mockFoo = (Foo<Bar>) mock(Foo.class);when(mockFoo.getValue()).thenReturn(new Bar());

View Article

Using Mockito to mock classes with generic parameters

Is there a clean method of mocking a class with generic parameters? Say I have to mock a class Foo<T> which I need to pass into a method that expects a Foo<Bar>. I can do the following...

View Article

Browsing latest articles
Browse All 13 View Live




<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>