Created
September 25, 2012 22:01
-
-
Save JavaDeveloper/3784746 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Suite; | |
import org.junit.runners.Suite.SuiteClasses; | |
@RunWith(Suite.class) | |
@SuiteClasses({Test1.class,Test2.class}) | |
public class ITTestSuite{ | |
private static final String IT_PERSISTANCE_UNIT = "it-persistence-unit"; | |
private static final String JDBC_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; | |
private static final String DB_URL = "jdbc:derby:memory:testDB;create=true"; | |
static EntityManagerFactory emf; | |
static EntityManager em | |
@BeforeClass | |
public static void setUp(){ | |
System.out.println("set up database"); | |
// 1. may this step is not required(to check it) | |
try { | |
Class.forName(JDBC_DRIVER); | |
DriverManager.getConnection(DB_URL).close(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
fail("Exception during database startup."); | |
} | |
// 2. create EntityManager | |
emf = Persistence.createEntityManagerFactory(mPersistenceUnit); | |
em = emf.createEntityManager(); | |
} | |
@AfterClass | |
public static void tearDown(){ | |
em.close(); | |
emf.close(); | |
System.out.println("tear down dababase"); | |
} | |
} | |
public class Test1{ | |
@Test | |
publicvoid testSomething(){ | |
EntityTransaction transaction = em.getTransaction(); | |
transaction.begin(); | |
ITTestSuite.em.persist( /*some entity*/ ); | |
... | |
ITTestSuite.em.merge( /*some entity*/ ); | |
// check that data has been persisted | |
assertTrue(...); | |
transaction.commit(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment