Skip to content

Instantly share code, notes, and snippets.

@JavaDeveloper
Created September 25, 2012 21:45
Show Gist options
  • Save JavaDeveloper/3784655 to your computer and use it in GitHub Desktop.
Save JavaDeveloper/3784655 to your computer and use it in GitHub Desktop.
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