@InjectMocks not working unless my test class extends another class

2,204 views
Skip to first unread message

Collin Peters

unread,
Sep 20, 2013, 12:51:25 PM9/20/13
to moc...@googlegroups.com
I am having a very strange problem. Basically it seems like @InjectMocks does not inject my @Mock unless my test class extends another class. Here is my code

@RunWith(MockitoJUnitRunner.class)
public class ClubConfigTest 
{
@Mock
private Observable<ClubConfigObserver> mockObservable;

@InjectMocks
private ClubConfig clubConfig;

@Test
public void testObserveChanges()
{
...
clubConfig.observeChanges(testClubConfigVO, originalClubConfigVO); 
verify(mockObservable, times(times)).getObservers();
}
...
}

public class ClubConfig
{
private Observable<ClubConfigObserver> observable = new Observable<>();

public ClubConfig()
{
}

public ClubConfig(Integer clubID)
{
this.clubID = clubID;
}
...

So I have snipped some stuff but there is enough info.  Basically the test as is fails on the verify. This is because the Observable variable is not a mock, but rather a regular instance. However, if I make it
public class ClubConfigTest extends FooTests
and FooTests is an empty class, then it works!!

I have no idea why this is. The Observable variable inside ClubConfig is obviously initialized at class instantiation, which might have an affect. But still... why would it work when the test class extends another?

KARR, DAVID

unread,
Sep 20, 2013, 1:43:26 PM9/20/13
to moc...@googlegroups.com

Do you have a setter method for “observable” in ClubConfig?  I would think that @InjectMocks would need that.

 

It might be worthwhile executing this code in the debugger.  Run it both with and without the base class.  When it gets into your CUT (code under test), look at the value of your “observable” variable.  I think in one case it’s going to look like a normal “Observable” instance, but in the other it’s going to look like a mocked object.  This will mostly just confirm what you’re seeing, but it’s useful to confirm this.

 

I would guess the complication is your initialization of the “observable” variable in the implicit constructor.  It almost seems like in the failure case, the constructor initialization is happening AFTER the mock is injected, but that makes no sense.

 

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/groups/opt_out.

Laurenţiu Lucian Petrea

unread,
Oct 1, 2013, 3:55:40 AM10/1/13
to moc...@googlegroups.com
This is a bug. I already submitted it as the defect 454 https://code.google.com/p/mockito/issues/detail?id=454&q=%40injectmocks. It is not the only issue with the @InjectMocks. It is a neat thing, but it needs some tuning. Help from the project members would be appreciated. 
Reply all
Reply to author
Forward
0 new messages