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"
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>

View File

@ -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);
}
}

View File

@ -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;
@ -39,7 +39,26 @@ public class HibernateManyToManyAnnotationMainIntegrationTest {
}
@Test
public void givenData_whenInsert_thenCreatesMtoMrelationship() {
public void givenSession_whenRead_thenReturnsMtoMdata() {
prepareData();
@SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list();
@SuppressWarnings("unchecked")
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());
}
}
private void prepareData() {
String[] employeeData = { "Peter Oven", "Allan Norman" };
String[] projectData = { "IT Project", "Networking Project" };
Set<Project> projects = new HashSet<Project>();
@ -50,27 +69,14 @@ public class HibernateManyToManyAnnotationMainIntegrationTest {
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() {
@SuppressWarnings("unchecked")
List<Employee> employeeList = session.createQuery("FROM Employee").list();
assertNotNull(employeeList);
for(Employee employee : employeeList) {
assertNotNull(employee.getProjects());
}
}
@After
public void tearDown() {
session.getTransaction()
.commit();
session.getTransaction().commit();
session.close();
}