|
| 1 | +package com.baeldung.hibernate.transaction; |
| 2 | + |
| 3 | +import com.baeldung.hibernate.HibernateUtil; |
| 4 | +import com.baeldung.hibernate.pojo.Post; |
| 5 | +import com.baeldung.hibernate.transaction.PostService; |
| 6 | +import org.hibernate.Session; |
| 7 | +import org.hibernate.SessionFactory; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import java.io.FileInputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.Properties; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
| 19 | +public class TransactionIntegrationTest { |
| 20 | + |
| 21 | + private static PostService postService; |
| 22 | + private static Session session; |
| 23 | + private static Logger logger = LoggerFactory.getLogger(TransactionIntegrationTest.class); |
| 24 | + |
| 25 | + @BeforeClass |
| 26 | + public static void init() throws IOException { |
| 27 | + Properties properties = new Properties(); |
| 28 | + properties.setProperty("hibernate.connection.driver_class", "org.h2.Driver"); |
| 29 | + properties.setProperty("hibernate.connection.url", "jdbc:h2:mem:mydb1;DB_CLOSE_DELAY=-1"); |
| 30 | + properties.setProperty("hibernate.connection.username", "sa"); |
| 31 | + properties.setProperty("hibernate.show_sql", "true"); |
| 32 | + properties.setProperty("jdbc.password", ""); |
| 33 | + properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); |
| 34 | + properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); |
| 35 | + SessionFactory sessionFactory = HibernateUtil.getSessionFactoryByProperties(properties); |
| 36 | + session = sessionFactory.openSession(); |
| 37 | + postService = new PostService(session); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenTitleAndBody_whenRepositoryUpdatePost_thenUpdatePost() { |
| 42 | + |
| 43 | + Post post = new Post("This is a title", "This is a sample post"); |
| 44 | + session.persist(post); |
| 45 | + |
| 46 | + String title = "[UPDATE] Java HowTos"; |
| 47 | + String body = "This is an updated posts on Java how-tos"; |
| 48 | + postService.updatePost(title, body, post.getId()); |
| 49 | + |
| 50 | + session.refresh(post); |
| 51 | + |
| 52 | + assertEquals(post.getTitle(), title); |
| 53 | + assertEquals(post.getBody(), body); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +} |
0 commit comments