2

I am learning Mockito , i have trouble understanding few things. Suppose i want to test a Doa method which gets List of objects and saves it in DB by iterating ove the list . How do test it using Mockito. Below is the code example.

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;


@Repository
public class AuditDaoImpl  {

    @PersistenceContext(unitName = "somepersistance")
    EntityManager entityManager;

    public <T> boolean saveAuditData(List<T> dataList) {
        Iterator<Data> itr = (Iterator<Data>) dataList.iterator();
        while (itr.hasNext()) {
            Data Data = (Data) itr.next();
            Audit audit = new Audit();
            audit.setUserName(Data.getUserName());
            entityManager.persist(audit);


        }
        return true;
    }

}
sumedha
  • 473
  • 1
  • 9
  • 24
  • 4
    You should not use Mockito for that. you should test it with an in memory db. – Jens Dec 01 '15 at 14:35
  • can you show what you tried so far, what effort did you make to write unit test? What exactly you want to test? – Milan Dec 01 '15 at 14:35
  • Mock the `EntityManager`. Call `saveAuditData` with a list of data. Verify that the `EntityManager` persists all data. – Manu Dec 01 '15 at 14:35
  • 2
    if you want to test 'unit test' that saveAuditData takes all your data and calls entity manager you can do that the way Manu said, if you want to test that your data is persisted correctly then follow Jens advice... it really depends what you want to test – Milan Dec 01 '15 at 14:39
  • @Mil4n I want to do as per Manu comments,unfortunately i dont know how to fill the list using Mock and call the entity manager in Mockito. – sumedha Dec 01 '15 at 14:50
  • 1
    you will create your 'dataListTest' set manually, the size of that is up to you, then you will call 'saveAuditData(dataListTest)' and verify that you called 'entityManager.persist(audit)' number of times as the size of your list - for instance check verify method invocation at: https://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html – Milan Dec 01 '15 at 15:01

2 Answers2

2

Assuming that your test class is annotated and running with spring (by using @RunWith(SpringJUnit4ClassRunner.class) and @ContextConfiguration("classpath:applicationContext.xml")) annotation and you have this working. And if your main concern is to verify that entityManager.persist(audit); is invoked for each element, something like this should work

@Autowired //Autowired to get mockito to inject into the spring-handled dao
@InjectMocks
AuditDaoImpl auditDao;

@Mock
EntityManager entityManager;

@Test
public void saveAllAudit_entityManagerShouldPersistAll() {
    List<Data> dataList = new ArrayList<>();
    dataList.add(new Data("username"));
    //add more to list

    auditDao.saveAuditData(dataList);

    verify(entityManager, times(1)).persist(anyObject());
}

If you actually need to test that it is persisted correctly, an in-memory database would be the way to go.

Martin Hansen
  • 2,033
  • 1
  • 18
  • 34
0

Mockito is poor for testing cases that deal with the persistence layer. You should use an embedded container to test the persistance layer. An embedded container is an in memory database that simulates your Database and is fast to create, making it ideal for unit tests.

Look at this SO question and read the SECOND answer :

Testing an EJB with JUnit

Community
  • 1
  • 1
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225