Never Inject Field with @Resource annotation(jsr250)

3,472 views
Skip to first unread message

jilen

unread,
Dec 25, 2011, 8:27:00 PM12/25/11
to mockito
Hi, today I use @InjectMocks annotation to inject a field annotated with
@Resource.
But when I run test case, null point exception is reported, seems not be
inject.
Shall we consider to inject such fields?
--

David Wallace

unread,
Dec 26, 2011, 3:49:17 AM12/26/11
to mockito
Hi Jilen,
Can you please post your code, so that we can see if there's any
reason why the field woudn't be injected?

jilen

unread,
Dec 26, 2011, 6:28:31 AM12/26/11
to moc...@googlegroups.com

public class JuUserInfoManagerImplPollCountTest extends MockitoTestCase {

    @Spy

    @InjectMocks

    JuUserInfoManagerImpl juUserInfoManagerImpl = new JuUserInfoManagerImpl();

    @Mock

    TairManager tairManager; //That is a cache System

    @Mock

    JuUserInfoDAO juUserInfoDAO;

    @Test

    public void testIncrAwardPollCount() throws ManagerException, DAOException {

        juUserInfoManagerImpl.incrAwardPollCount(1L, 2);

        verify(juUserInfoDAO).incrUserPollCount(1L, 2, null);

        verify(tairManager).delete(MyJuCacheArea.MY_USER_POLL_COUNT,JuUserInfoManager.POLL_COUNT_PREFIX + 1L);

    }

}

public class JuUserInfoManagerImpl implements JuUserInfoManager {

    private static final Logger log = LoggerFactory.getLogger(JuUserInfoManagerImpl.class);

    @Autowired

    @Resource(name="tairManager")

    private TairManager tairManager;

    @Override

    public void incrAwardPollCount(long userId, int pollCount) throws ManagerException {

    try {

        juUserInfoDAO.incrUserPollCount(userId, pollCount, null);

        clearPollCountCache(userId);//null point here..................................

    } catch (DAOException e) {

    log.error("[JuUserInfoManagerImpl-incrAwardPollCount]");

    throw new ManagerException("[JuUserInfoManagerImpl-incrAwardPollCount]",e);

    }

}


private void clearPollCountCache(long userId){

    tairManager.delete(MyJuCacheArea.MY_USER_POLL_COUNT, POLL_COUNT_PREFIX + userId); //null pointer here

--

jilen

unread,
Dec 26, 2011, 7:03:43 AM12/26/11
to moc...@googlegroups.com
Very Sorry! I take another class with different package name.
So, mockito found no mock to inject to that field.
Very sorry about this silly mistake.
--

Brice Dutheil

unread,
Dec 27, 2011, 12:18:57 PM12/27/11
to moc...@googlegroups.com
Hi Jilen,

Please note that Mockito has absolutely no knowledge of other frameworks / JSR annotations like @Inject, or @Resource, or something else. It just try to inject mocks wherever it is the most appropriate and without configuration, it certainly isn't perfect, but it shall deal fine in most use cases.




A few other remarks you should name your class with a meaningful name instead of appending "Impl" or prepending your interface with "I", this way you have less chance to mix different types.

Also instead of using a super class MockitoTestCase, you might want to use the following JUnit runner declaration on your actual test class:
@RunWith(MockitoJUnitRunner.class)



Cheers,



-- 
Brice


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.

sign.png
Reply all
Reply to author
Forward
0 new messages