Mocking two objects of the same type with Mockito

I am writing unit tests using Mockito and am having trouble mocking the classes introduced. The problem is that two of the classes introduced are the same type and only differentiate by their @Qualifier annotation. If I tried to just make fun of SomeClass.class , this layout will not be entered and this object will be null in my tests. How can I mock these objects?

 public class ProfileDAL { @Inject @Qualifier("qualifierA") private SomeClass someClassA ; @Inject @Qualifier("qualifierB") private SomeClass someClassB ; //...various code, not important } @RunWith(MockitoJUnitRunner.class) public class ProfileDALLOMImplTest { @InjectMocks private ProfileDALLOMImpl profileDALLOMImpl = new ProfileDALLOMImpl(); @Mock private SomeClass someClassA; @Mock private SomeClass someClassB; private SomeResult mockSomeResult = mock(SomeResult.class); @Test public void testSomeMethod() { when(someClassA .getSomething(any(SomeArgment.class)).thenReturn(mockSomeResult); Int result = profileDALLOMImpl.someTest(This isn't relevant); } } 
+8
mockito
source share
4 answers

I tried to mock two objects of the same type with Mockito 1.9.5 using JUnit and it works.

See: http://static.javadoc.io/org.mockito/mockito-core/1.9.5/org/mockito/InjectMocks.html

Information about the corresponding type from the document:

"Entering fields, mocks will first be allowed by type, then if there are several properties of the same type, the field name and layout name match."

And this one that seems to make the false name match the field name for all your layouts when you have two of the same type:

"Note 1: If you have fields of the same type (or with the same erasure), it is better to name all @Mock annotated fields @Mock appropriate fields, otherwise Mockito may get confused and injection will not happen."

Perhaps this last one bites you?

+9
source share

Just confirmed what Splonk said, and so it works in Mockito 1.9.5, as soon as I deleted one of the mocking classes, it failed.

So, in your case, make sure you have both mocking classes with the same name as the class in your test:

 @Mock private SomeClass someClassA; @Mock private SomeClass someClassB; 
+1
source share

If you do not use annotation you will get something like

 public class MyClass { private MyDependency myDependency; public void setMyDependency(MyDependency myDependency){ this.myDependency = myDependency; } } 

and

 import org.junit.Before; import org.junit.Test; import static org.mockito.Mockito.*; public class MyTest { private MyClass myClass; private MyDependency myDependency; @Before public void setUp(){ myClass = new MyClass(); myDependency = mock(MyDependency.class); myClass.setMyDependency(myDependency); } @Test public void test(){ // Given // When // Then } } 

You can do the same if your object has its dependencies defined by the constructor, and not through setter. I believe that your dependency injection infrastructure can annotate setters the same way you comment on private fields, but now your tests are not dependent on any dependency injection scheme.

0
source share

When all hope does not pass, you can always use powermock to enter private fields. The following link has a clear guide: Mockito: False initialization of a private field

0
source share

All Articles