Fix to Unit test for Hibernate one to many (#1085)

* Fix to Unit tests for Hibernate one to many

* Fixed Unit test for hibernate one to many mapping tutorial

* Fixed Unit test for Hibernate one to many mapping tutorial

* Fixed Unit test for Hibernate one to many mapping Tutorial

* Fixed Unit Test for Hibernate one to many mapping Tutorial

* Unit test for Hibernate One to many Tutorial
This commit is contained in:
Mobi Blunt 2017-02-02 17:23:24 +01:00 committed by maibin
parent 3522c0537b
commit c350e8b423
6 changed files with 70 additions and 304 deletions

View File

@ -5,15 +5,15 @@
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">bastard5</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/setup</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">mypassword</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_one_to_many?createDatabaseIfNotExist=true</property>
<property name="hibernate.connection.username">myuser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<mapping class="com.baeldung.hibernate.model.Cart"/>
<mapping class="com.baeldung.hibernate.model.Items"/>
<mapping class="com.baeldung.hibernate.oneToMany.model.Cart"/>
<mapping class="com.baeldung.hibernate.oneToMany.model.Items"/>
</session-factory>
</hibernate-configuration>

View File

@ -1,43 +0,0 @@
package com.baeldung.hibernate.oneToMany.config;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class HibernateAnnotationUtilTest {
public HibernateAnnotationUtilTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testGetSessionFactory() {
System.out.println("getSessionFactory");
SessionFactory expResult = null;
SessionFactory result = HibernateAnnotationUtil.getSessionFactory();
assertEquals(expResult, result);
fail("The test failed.");
}
}

View File

@ -1,8 +1,19 @@
package com.baeldung.hibernate.oneToMany.main;
import org.junit.After;
import org.junit.AfterClass;
import com.baeldung.hibernate.oneToMany.model.Cart;
import com.baeldung.hibernate.oneToMany.model.Items;
import java.util.HashSet;
import java.util.Set;
import static org.hamcrest.Matchers.hasSize;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.service.ServiceRegistry;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -11,30 +22,68 @@ import static org.junit.Assert.*;
public class HibernateOneToManyAnnotationMainTest {
private static SessionFactory sessionFactory;
private Session session;
public HibernateOneToManyAnnotationMainTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
@BeforeClass
public static void beforeTests() {
Configuration configuration = new Configuration().addAnnotatedClass(Cart.class).addAnnotatedClass(Items.class).setProperty("hibernate.dialect", HSQLDialect.class.getName()).setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName())
.setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test").setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "").setProperty("hibernate.hbm2ddl.auto", "update");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
@Before
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@After
public void tearDown() {
}
@Test
public void testMain() {
System.out.println("main");
String[] args = null;
HibernateOneToManyAnnotationMain.main(args);
fail("The test failed.");
public void testAddItemsToCart() {
Cart cart = new Cart();
Set <Items> cartItems = new HashSet<>();
cartItems = cart.getItems();
Assert.assertNull(cartItems);
Items item1 = new Items("I10", 10, 1, cart);
assertNotNull(item1);
Set<Items> itemsSet = new HashSet<Items>();
cart.setItems(itemsSet);
assertNotNull(cart);
System.out.println("Items added to cart");
}
@Test
public void testSaveCart(){
Cart cart = new Cart();
Set <Items> cartItems = new HashSet<>();
cartItems = cart.getItems();
Assert.assertNull(cartItems);
Items item1 = new Items();
item1.setItemId("I10");
item1.setItemTotal(10);
item1.setQuantity(1);
item1.setCart(cart);
assertNotNull(item1);
Set<Items> itemsSet = new HashSet<Items>();
itemsSet.add(item1);
assertNotNull(itemsSet);
cart.setItems(itemsSet);
assertNotNull(cart);
session.persist(cart);
session.getTransaction().commit();
session.close();
}
}

View File

@ -1,111 +0,0 @@
package com.baeldung.hibernate.oneToMany.model;
import java.util.Set;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class CartTest {
public CartTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testGetId() {
System.out.println("getId");
Cart instance = new Cart();
long expResult = 0L;
long result = instance.getId();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetId() {
System.out.println("setId");
long id = 0L;
Cart instance = new Cart();
instance.setId(id);
fail("The test failed.");
}
@Test
public void testGetTotal() {
System.out.println("getTotal");
Cart instance = new Cart();
double expResult = 0.0;
double result = instance.getTotal();
assertEquals(expResult, result, 0.0);
fail("The test failed.");
}
@Test
public void testSetTotal() {
System.out.println("setTotal");
double total = 0.0;
Cart instance = new Cart();
instance.setTotal(total);
fail("The test failed.");
}
@Test
public void testGetName() {
System.out.println("getName");
Cart instance = new Cart();
String expResult = "";
String result = instance.getName();
assertEquals(expResult, result);
fail("The test failed");
}
@Test
public void testSetName() {
System.out.println("setName");
String name = "";
Cart instance = new Cart();
instance.setName(name);
fail("The test failed.");
}
@Test
public void testGetItems() {
System.out.println("getItems");
Cart instance = new Cart();
Set<Items> expResult = null;
Set<Items> result = instance.getItems();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetItems() {
System.out.println("setItems");
Set<Items> items = null;
Cart instance = new Cart();
instance.setItems(items);
fail("The test case failed");
}
}

View File

@ -1,129 +0,0 @@
package com.baeldung.hibernate.oneToMany.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class ItemsTest {
public ItemsTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void testGetItemId() {
System.out.println("getItemId");
Items instance = new Items();
String expResult = "";
String result = instance.getItemId();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetItemId() {
System.out.println("setItemId");
String itemId = "";
Items instance = new Items();
instance.setItemId(itemId);
fail("The test failed.");
}
@Test
public void testGetItemTotal() {
System.out.println("getItemTotal");
Items instance = new Items();
double expResult = 0.0;
double result = instance.getItemTotal();
assertEquals(expResult, result, 0.0);
fail("The test failed.");
}
@Test
public void testSetItemTotal() {
System.out.println("setItemTotal");
double itemTotal = 0.0;
Items instance = new Items();
instance.setItemTotal(itemTotal);
fail("The test failed.");
}
@Test
public void testGetQuantity() {
System.out.println("getQuantity");
Items instance = new Items();
int expResult = 0;
int result = instance.getQuantity();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetQuantity() {
System.out.println("setQuantity");
int quantity = 0;
Items instance = new Items();
instance.setQuantity(quantity);
fail("The test failed.");
}
@Test
public void testGetCart() {
System.out.println("getCart");
Items instance = new Items();
Cart expResult = null;
Cart result = instance.getCart();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetCart() {
System.out.println("setCart");
Cart cart = null;
Items instance = new Items();
instance.setCart(cart);
fail("The test failed.");
}
@Test
public void testGetId() {
System.out.println("getId");
Items instance = new Items();
long expResult = 0L;
long result = instance.getId();
assertEquals(expResult, result);
fail("The test failed.");
}
@Test
public void testSetId() {
System.out.println("setId");
long id = 0L;
Items instance = new Items();
instance.setId(id);
fail("The test failed.");
}
}