How do I write a Junit test case for DAO layer in Spring by using Mockito and by creating a Mock object of EntityManager?
-
I want to write a Junit test case for Dao layer in Spring using Mockito by creating mock object of EntityManager. Below is the sample code: public CategoryList getAllCategoryByMerchant(String merchantId) throws PersistenceException, Exception { CategoryList categoryList = new CategoryList(); EntityManager manager = factory.createEntityManager(); List<Category> tempCategoryList = manager .createQuery(DBQueries.GET_ALL_CATEGORY_BY_MERCHANT, Category.class).setParameter(1, merchantId) .getResultList(); categoryList.setCategoryList(tempCategoryList); manager.close(); return categoryList; } I am trying to write Junit test case: @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); //Important! Mocks need to be initialized. factory=mock(EntityManagerFactory.class); entityManager=mock(EntityManager.class); criteriaBuilder=mock(CriteriaBuilder.class); criteriaQuery=mock(CriteriaQuery.class); categoryDao =new CategoryDaoImpl(); } i want to write test case for above method: getAllCategoriesByStore() i write some code shown below: @Test public void testGetAllCategoryByMerchant() throws PersistenceException, Exception { CategoryList categoryList =new CategoryList(); List<Category> categories= new ArrayList<Category>(); Category category1=new Category(); category1.setCategoryName("abc"); categories.set(1,category1); categoryList.setCategoryList(categories); Mockito.when(factory.createEntityManager()).thenReturn(entityManager); Mockito.when(categoryDao.getAllCategoryByMerchant("")).thenReturn(categoryList); assert (categoryList.getCategoryList().size() > 0); }
-
Answer:
In the above code, you are testing whether Java's ArrayList implementation has an appropriate size() function, which is probably not what you want: List<Category> categories= new ArrayList<Category>(); \\ Add some element to this list, then... assert (categoryList.getCategoryList().size() > 0); If the object under test is supposed to be categoryDao (which is suggested by the object instantiation in your setUp() method), then you will want to verify the interactions of THAT class. What you've done is to create a categoryDao and then have Mockito try and mock it (which must be throwing an error!) This line is the issue: Mockito.when(categoryDao.getAllCategoryByMerchant("[email protected]")).thenReturn(categoryList); You want your test along these lines: // Set up your test: String merchantId = "some-merchant-id"; List<Category> expectedCategoryList = mock(List.class); when(factory.createEntityManager()).thenReturn(entityManager); when(manager.createQuery(DBQueries.GET_ALL_CATEGORY_BY_MERCHANT, Category.class)).thenReturn(query); when(query.setParameter(1, merchantId)).thenReturn(expectedCategoryList); // Execute the behavior you want to test: categoryDao =new CategoryDaoImpl(); CategoryList result = categoryDao.getAllCategoryByMerchant(merchantId); // Verify you got what you wanted: assertEquals(result.getCategoryList(), expectedCategoryList); verify(manager).close();
Josh Lospinoso at Quora Visit the source
Other answers
I recommand you to Inject the entitymanager with spring, not the factory... So that you can mock EM calls, you're not constrained to create a mock implementation of EM then mock factory EM creation
Charlie Mordant
Related Q & A:
- How do I write a good peer review?Best solution by Quora
- How do I write a search page?Best solution by Stack Overflow
- How can I write a good reference letter?Best solution by dailywritingtips.com
- How do I write a Photography Price Quote?Best solution by Yahoo! Answers
- How do I write a proper film review?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.