The moose likes EJB and other Java EE Technologies and the fly likes Testing EJB Local Bean with Mockito Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Testing EJB Local Bean with Mockito" Watch "Testing EJB Local Bean with Mockito" New topic
Author

Testing EJB Local Bean with Mockito

Shashi Shankar
Greenhorn

Joined: Dec 27, 2012
Posts: 4
Hi All,
I am getting some nul pointer exception while testing my EJB local bean with mockito.
I am using mockito to mock entityManager for the local bean.




I am getting null pointer exception for the line 41 of local bean:
List<SourceDestination> sourceDestList = getSourceDestinationQuery.getResultList();

So any inputs to solve this issue will be really helpful.

Thanks in advance.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

You have to record your mock. Notice that you invoke:

entityManager.createQuery(getSourceDestination, SourceDestination.class)

However your mock is not prepared for such invocation and it return null (with EasyMock you could even get Exception) - like nice mock.

To sum up, you should record your mock:
when(X).thenReturn(Y);

X - your invocation,
Y - can be empty list or another mock.



SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Shashi Shankar
Greenhorn

Joined: Dec 27, 2012
Posts: 4
Thanks for reply Smith,
As per your suggestion I need to record my mock object. But still I am not getting how can I execute my test case as mentioned in earlier post.

It would be really helpful if you provide me some work around.

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I do not know what you want to achieve. You can run the test from Eclipse (Run as JUnit Test) or you can run it within Maven build process.

Please, precise what your problem really is.
 
 
subject: Testing EJB Local Bean with Mockito