JAVA-10379 Fix tests in hibernate-mapping-2 module

This commit is contained in:
anuragkumawat 2022-03-01 18:13:26 +05:30
parent c87c79e035
commit 36899fd0e3
3 changed files with 57 additions and 44 deletions

View File

@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>hibernate-mapping-2y</artifactId> <artifactId>hibernate-mapping-2</artifactId>
<version>0.1-SNAPSHOT</version> <version>0.1-SNAPSHOT</version>
<name>hibernate-mapping-2</name> <name>hibernate-mapping-2</name>

View File

@ -45,6 +45,13 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest {
projects.add(new Project("Networking Project")); projects.add(new Project("Networking Project"));
session.persist(new Employee("Peter", "Oven", projects)); session.persist(new Employee("Peter", "Oven", projects));
session.persist(new Employee("Allan", "Norman", 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);
} }
} }

View File

@ -1,7 +1,7 @@
package com.baeldung.hibernate.manytomany; package com.baeldung.hibernate.manytomany;
import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -23,60 +23,66 @@ import com.baeldung.manytomany.util.HibernateUtil;
* Configured in: manytomany.cfg.xml * Configured in: manytomany.cfg.xml
*/ */
public class HibernateManyToManyAnnotationMainIntegrationTest { public class HibernateManyToManyAnnotationMainIntegrationTest {
private static SessionFactory sessionFactory; private static SessionFactory sessionFactory;
private Session session; private Session session;
@BeforeClass @BeforeClass
public static void beforeTests() { public static void beforeTests() {
sessionFactory = HibernateUtil.getSessionFactory(); sessionFactory = HibernateUtil.getSessionFactory();
} }
@Before @Before
public void setUp() { public void setUp() {
session = sessionFactory.openSession(); session = sessionFactory.openSession();
session.beginTransaction(); session.beginTransaction();
} }
@Test @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
public void givenSession_whenRead_thenReturnsMtoMdata() { public void givenSession_whenRead_thenReturnsMtoMdata() {
prepareData();
@SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list(); List<Project> projectList = session.createQuery("FROM Project").list();
assertNotNull(employeeList); assertNotNull(employeeList);
assertNotNull(projectList);
assertEquals(2, employeeList.size());
assertEquals(2, projectList.size());
for(Employee employee : employeeList) { for(Employee employee : employeeList) {
assertNotNull(employee.getProjects()); assertNotNull(employee.getProjects());
} }
for(Project project : projectList) {
assertNotNull(project.getEmployees());
}
} }
@After private void prepareData() {
public void tearDown() { String[] employeeData = { "Peter Oven", "Allan Norman" };
session.getTransaction() String[] projectData = { "IT Project", "Networking Project" };
.commit(); Set<Project> projects = new HashSet<Project>();
session.close();
}
@AfterClass for (String proj : projectData) {
public static void afterTests() { projects.add(new Project(proj));
sessionFactory.close(); }
}
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();
}
} }