Created
March 13, 2024 16:53
-
-
Save FranckSilvestre/8f900790c9b601df7e6d5523d394c210 to your computer and use it in GitHub Desktop.
v1 in OurBusinessProject 2023-2024
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
package ourbusinessproject; | |
import jakarta.persistence.EntityManager; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
import org.mockito.Mock; | |
import org.mockito.MockitoAnnotations; | |
import org.mockito.junit.jupiter.MockitoExtension; | |
import static org.mockito.Mockito.verify; | |
@ExtendWith(MockitoExtension.class) | |
class EnterpriseProjectServiceTest { | |
private EnterpriseProjectService enterpriseProjectService; | |
@Mock | |
private EntityManager entityManager; | |
private Project project; | |
private Enterprise enterprise; | |
private Long anId = 1L; | |
@BeforeEach | |
public void setUp() throws Exception { | |
enterpriseProjectService = new EnterpriseProjectService(entityManager); | |
} | |
@Test | |
public void testEntityManagerPersistAProjectWhenANewProjectIsCreated() { | |
// when: trying to create a project | |
project = enterpriseProjectService.newProject("a title", "a description"); | |
// then: the service delegates the entity manager to persist the project | |
verify(enterpriseProjectService.getEntityManager()).persist(project); | |
// and: the service asks the entity manager to synchronize with the database | |
verify(entityManager).flush(); | |
} | |
@Test | |
public void testEntityManagerPersistAnEnterpriseWhenEnterpriseIsSaved() { | |
// when: trying to create an enterprise | |
enterprise = enterpriseProjectService.newEnterprise( | |
"a name", | |
"a description", | |
"a contact name", | |
"[email protected]" | |
); | |
// then: the persist method is invoked on the entity manager | |
verify(entityManager).persist(enterprise); | |
// and: the service asks the entity manager to synchronize with the database | |
verify(entityManager).flush(); | |
} | |
@Test | |
public void testEntityManagerFindAProjectWhenProjectIsSearchedById() { | |
// when: trying to save the project | |
project = enterpriseProjectService.findProjectById(anId); | |
// then: the service delegates the entity manager | |
verify(entityManager).find(Project.class, anId); | |
} | |
@Test | |
public void testEntityManagerFindAnEnterpriseWhenEnterpriseIsSearchedById() { | |
// when: trying to find an enterprise | |
enterprise = enterpriseProjectService.findEnterpriseById(anId); | |
// then: the service delegates the entity manager | |
verify(entityManager).find(Enterprise.class, anId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment