Merge pull request #10620 from kwoyke/JAVA-5083
JAVA-5083: Fix unit tests in java-jpa-3 module
This commit is contained in:
commit
c4c8ef6d27
@ -9,6 +9,7 @@ import org.hibernate.cfg.Environment;
|
|||||||
import org.hibernate.service.ServiceRegistry;
|
import org.hibernate.service.ServiceRegistry;
|
||||||
|
|
||||||
public class HibernateConfig {
|
public class HibernateConfig {
|
||||||
|
|
||||||
private static SessionFactory sessionFactory;
|
private static SessionFactory sessionFactory;
|
||||||
|
|
||||||
public static SessionFactory getSessionFactory() {
|
public static SessionFactory getSessionFactory() {
|
||||||
@ -16,12 +17,12 @@ public class HibernateConfig {
|
|||||||
Configuration configuration = new Configuration();
|
Configuration configuration = new Configuration();
|
||||||
|
|
||||||
Properties settings = new Properties();
|
Properties settings = new Properties();
|
||||||
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
|
settings.put(Environment.DRIVER, "org.h2.Driver");
|
||||||
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/app_db?useSSL=false");
|
settings.put(Environment.URL, "jdbc:h2:mem:test");
|
||||||
settings.put(Environment.USER, "root");
|
settings.put(Environment.USER, "sa");
|
||||||
settings.put(Environment.PASS, "password");
|
settings.put(Environment.PASS, "");
|
||||||
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
|
settings.put(Environment.DIALECT, "org.hibernate.dialect.H2Dialect");
|
||||||
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
|
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
|
||||||
configuration.setProperties(settings);
|
configuration.setProperties(settings);
|
||||||
|
|
||||||
configuration.addAnnotatedClass(User.class);
|
configuration.addAnnotatedClass(User.class);
|
||||||
|
@ -1,19 +1,24 @@
|
|||||||
package com.baeldung.ignorable.fields;
|
package com.baeldung.ignorable.fields;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.MapperFeature;
|
import com.fasterxml.jackson.databind.MapperFeature;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
public class TransientFieldUnitTest {
|
public class TransientFieldUnitTest {
|
||||||
|
|
||||||
@ -23,7 +28,6 @@ public class TransientFieldUnitTest {
|
|||||||
|
|
||||||
private final User user = new User("user" + randInt + "@bar.com", "hunter2", "MacOSX");
|
private final User user = new User("user" + randInt + "@bar.com", "hunter2", "MacOSX");
|
||||||
|
|
||||||
@Ignore
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFieldWithTransientAnnotation_whenSavingViaJPA_thenFieldIgnored() {
|
public void givenFieldWithTransientAnnotation_whenSavingViaJPA_thenFieldIgnored() {
|
||||||
userDao.saveUser(user);
|
userDao.saveUser(user);
|
||||||
@ -36,18 +40,20 @@ public class TransientFieldUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenFieldWithTransientAnnotation_whenSerializingObject_thenFieldSerialized() throws IOException, ClassNotFoundException {
|
public void givenFieldWithTransientAnnotation_whenSerializingObject_thenFieldSerialized() throws IOException, ClassNotFoundException {
|
||||||
|
|
||||||
FileOutputStream fout = new FileOutputStream("test.obj");
|
try (FileOutputStream fout = new FileOutputStream("test.obj")) {
|
||||||
ObjectOutputStream out = new ObjectOutputStream(fout);
|
ObjectOutputStream out = new ObjectOutputStream(fout);
|
||||||
out.writeObject(user);
|
out.writeObject(user);
|
||||||
out.flush();
|
out.flush();
|
||||||
out.close();
|
}
|
||||||
|
|
||||||
FileInputStream fin = new FileInputStream("test.obj");
|
try (FileInputStream fin = new FileInputStream("test.obj")) {
|
||||||
ObjectInputStream in = new ObjectInputStream(fin);
|
ObjectInputStream in = new ObjectInputStream(fin);
|
||||||
User savedUser = (User) in.readObject();
|
User savedUser = (User) in.readObject();
|
||||||
in.close();
|
|
||||||
|
|
||||||
assertEquals(user.getCurrentDevice(), savedUser.getCurrentDevice());
|
assertEquals(user.getCurrentDevice(), savedUser.getCurrentDevice());
|
||||||
|
}
|
||||||
|
|
||||||
|
Files.deleteIfExists(Paths.get("test.obj"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user