Created
November 1, 2012 16:18
-
-
Save chineseneo/3994740 to your computer and use it in GitHub Desktop.
ProjectDAO with EntityManagerFactory dependency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.beans.factory.annotation.Autowired; | |
| import org.springframework.beans.factory.annotation.Qualifier; | |
| import org.springframework.stereotype.Repository; | |
| import javax.persistence.*; | |
| import java.util.List; | |
| @Repository("projectDAO") | |
| public class ProjectDAOImpl implements ProjectDAO { | |
| @PersistenceUnit | |
| private EntityManagerFactory entityManagerFactory; | |
| @Autowired(required = true) | |
| public ProjectDAOImpl(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) { | |
| this.entityManagerFactory = entityManagerFactory; | |
| } | |
| @Override | |
| public Project save(Project project) { | |
| EntityManager entityManager = entityManagerFactory.createEntityManager(); | |
| try { | |
| entityManager.getTransaction().begin(); | |
| entityManager.persist(project); | |
| entityManager.getTransaction().commit(); | |
| return project; | |
| } | |
| finally { | |
| if (entityManager != null) { | |
| entityManager.close(); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.Before; | |
| import org.junit.Test; | |
| import javax.persistence.EntityManager; | |
| import javax.persistence.EntityManagerFactory; | |
| import javax.persistence.EntityTransaction; | |
| import javax.persistence.Query; | |
| import static org.mockito.Mockito.*; | |
| public class ProjectDAOImplTest { | |
| private Project project; | |
| private ProjectDAOImpl projectDAO; | |
| private EntityManager entityManager; | |
| @Before | |
| public void setUp() { | |
| project = new Project(0, "My Project", "description", "image/path",true,"image/path","summary"); | |
| entityManager = mock(EntityManager.class); | |
| EntityManagerFactory entityManagerFactory = mock(EntityManagerFactory.class); | |
| when(entityManagerFactory.createEntityManager()).thenReturn(entityManager); | |
| projectDAO = new ProjectDAOImpl(entityManagerFactory); | |
| } | |
| @Test | |
| public void shouldSaveAProject() { | |
| EntityTransaction transaction = mock(EntityTransaction.class); | |
| when(entityManager.getTransaction()).thenReturn(transaction); | |
| projectDAO.save(project); | |
| verify(entityManager).persist(project); | |
| verify(entityManager).close(); | |
| verify(transaction).begin(); | |
| verify(transaction).commit(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment