(我知道人的复数是人,我更喜欢人..)
我试图了解测试的本质。我有一个玩具项目,在这个玩具项目中,我有以下课程:
package biz.tugay.jpaExamples.dao;
import biz.tugay.jpaExamples.model.Person;
import biz.tugay.jpaExamples.service.EntityManagerService;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.List;
public final class PersonDaoImpl {
private EntityManagerService entityManagerService;
public List<Person> getAll() {
final EntityManager entityManager = entityManagerService.entityManager();
final TypedQuery<Person> selectAllPersons = entityManager.createQuery("SELECT p FROM Person p", Person.class);
final List<Person> persons = selectAllPersons.getResultList();
entityManager.close();
return persons;
}
public void setEntityManagerService(final EntityManagerService entityManagerService) {
this.entityManagerService = entityManagerService;
}
}
当我运行我的项目时,此方法可以正常工作。我向它注入一个EntityManagerService
,当我在getAll()
中调用PersonDaoImpl
时,我将从数据库中获取Persons
。这是我对EntityManagerService
的实施,这与我认为的问题无关:
package biz.tugay.jpaExamples.service;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public final class EntityManagerServiceByPersistenceUnitNameImpl implements EntityManagerService {
private final EntityManagerFactory entityManagerFactory;
public EntityManagerServiceByPersistenceUnitNameImpl(final String persistenceUnitName) {
this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName);
}
@Override
public EntityManager entityManager() {
final EntityManager entityManager = entityManagerFactory.createEntityManager();
return entityManager;
}
@Override
public void shutDown() {
entityManagerFactory.close();
}
}
因为我正在尝试学习/理解单元测试,我想为PersonDaoImpl
创建一个单元测试。这里是:
package biz.tugay.jpaExamples.dao;
import biz.tugay.jpaExamples.model.Person;
import biz.tugay.jpaExamples.service.EntityManagerService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;
@RunWith(MockitoJUnitRunner.class)
public class PersonDaoImplTest {
@InjectMocks
private final PersonDaoImpl personDao = new PersonDaoImpl();
@Mock
private EntityManagerService entityManagerService;
@Mock
private EntityManager entityManager;
@Mock
private TypedQuery<Person> typedQuery;
@Test
public void testGetAll() throws Exception {
// Given we have 1 person in the database..
final List<Person> persons = new ArrayList<Person>();
final Person person = new Person();
person.setFirstname("Koray");
person.setLastname("Tugay");
persons.add(person);
// Given entityManagerService returns a valid EntityManager
Mockito.when(entityManagerService.entityManager()).thenReturn(entityManager);
// Given entityManager creates a valid TypedQuery
Mockito.when(entityManager.createQuery("SELECT p FROM Person p", Person.class)).thenReturn(typedQuery);
// Given typedQuery returns the persons from the database when getResultList is called
Mockito.when(typedQuery.getResultList()).thenReturn(persons);
// When personDao.getAll is called..
final List<Person> all = personDao.getAll();
// Then the returned List must be of size 1.
Assert.assertTrue(all.size() == 1);
// And the person in the List returned from the typedQuery must be equal to the person in the initial List.
final Person returnedPerson = all.get(0);
Assert.assertTrue(returnedPerson.getFirstname().equals("Koray"));
Assert.assertTrue(returnedPerson.getLastname().equals("Tugay"));
Mockito.verify(entityManager).close();
}
}
这是一个有效的单元测试吗?如果没有,是因为我在PersonDaoImpl
类中设计不好,我该如何正确地测试这个方法呢?
如果是的话,你能澄清一下它真正的测试内容以及它如何帮助我重构或让我确信我做的正确/我正在做我正在做的事情吗?
因为目前,我认为写这个测试绝对没有任何好处,但我很确定这是我的错,所以我试图理解。