↧
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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleAnswer 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 ArticleUsing 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
More Pages to Explore .....