JAVA-10379 Fix tests in hibernate-mapping-2 module
This commit is contained in:
parent
c87c79e035
commit
36899fd0e3
|
@ -3,7 +3,7 @@
|
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>hibernate-mapping-2y</artifactId>
|
||||
<artifactId>hibernate-mapping-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>hibernate-mapping-2</name>
|
||||
|
||||
|
|
|
@ -45,6 +45,13 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
|
|||
projects.add(new Project("Networking Project"));
|
||||
session.persist(new Employee("Peter", "Oven", projects));
|
||||
session.persist(new Employee("Allan", "Norman", projects));
|
||||
|
||||
Set<Employee> employees = new HashSet<Employee>();
|
||||
employees.add(new Employee("Sam", "Curran"));
|
||||
employees.add(new Employee("Tom", "Curran"));
|
||||
Project project = new Project("Database Project");
|
||||
project.setEmployees(employees);
|
||||
session.persist(project);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.hibernate.manytomany;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
@ -23,60 +23,66 @@ import com.baeldung.manytomany.util.HibernateUtil;
|
|||
* Configured in: manytomany.cfg.xml
|
||||
*/
|
||||
public class HibernateManyToManyAnnotationMainIntegrationTest {
|
||||
private static SessionFactory sessionFactory;
|
||||
private static SessionFactory sessionFactory;
|
||||
|
||||
private Session session;
|
||||
private Session session;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory();
|
||||
}
|
||||
@BeforeClass
|
||||
public static void beforeTests() {
|
||||
sessionFactory = HibernateUtil.getSessionFactory();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
@Before
|
||||
public void setUp() {
|
||||
session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenData_whenInsert_thenCreatesMtoMrelationship() {
|
||||
String[] employeeData = { "Peter Oven", "Allan Norman" };
|
||||
String[] projectData = { "IT Project", "Networking Project" };
|
||||
Set<Project> projects = new HashSet<Project>();
|
||||
|
||||
for (String proj : projectData) {
|
||||
projects.add(new Project(proj));
|
||||
}
|
||||
|
||||
for (String emp : employeeData) {
|
||||
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
|
||||
assertEquals(0, employee.getProjects().size());
|
||||
employee.setProjects(projects);
|
||||
session.persist(employee);
|
||||
assertNotNull(employee);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test
|
||||
public void givenSession_whenRead_thenReturnsMtoMdata() {
|
||||
prepareData();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Employee> employeeList = session.createQuery("FROM Employee").list();
|
||||
List<Project> projectList = session.createQuery("FROM Project").list();
|
||||
assertNotNull(employeeList);
|
||||
assertNotNull(projectList);
|
||||
assertEquals(2, employeeList.size());
|
||||
assertEquals(2, projectList.size());
|
||||
|
||||
for(Employee employee : employeeList) {
|
||||
assertNotNull(employee.getProjects());
|
||||
}
|
||||
for(Project project : projectList) {
|
||||
assertNotNull(project.getEmployees());
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.getTransaction()
|
||||
.commit();
|
||||
session.close();
|
||||
}
|
||||
private void prepareData() {
|
||||
String[] employeeData = { "Peter Oven", "Allan Norman" };
|
||||
String[] projectData = { "IT Project", "Networking Project" };
|
||||
Set<Project> projects = new HashSet<Project>();
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
for (String proj : projectData) {
|
||||
projects.add(new Project(proj));
|
||||
}
|
||||
|
||||
for (String emp : employeeData) {
|
||||
Employee employee = new Employee(emp.split(" ")[0], emp.split(" ")[1]);
|
||||
employee.setProjects(projects);
|
||||
session.persist(employee);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterTests() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue