我想用这个单元测试我的DAO实现:
public class BaseDAOImplTest {
private EntityManagerFactory emFactory;
private EntityManager em;
private static Logger logger = Logger.getLogger(BaseDAOImplTest.class
.getName());
@Before
public void setUp() throws SQLException, ClassNotFoundException {
logger.info("Starting in-memory database for unit tests");
try {
//Creating testDB database
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
DriverManager.getConnection("jdbc:derby:memory:TestingDB;create=true");
logger.info("Building JPA EntityManager for unit tests");
//Creating Entity Manager
emFactory = Persistence.createEntityManagerFactory("TestingDB");
em = emFactory.createEntityManager();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
@Test
public void testPersistExcursion() {
BaseDAOImpl baseDao = new BaseDAOImpl(Excursion.class, em);
Calendar c = Calendar.getInstance();
c.set(2013, 8, 24);
Excursion ex1 = createExcursion(c.getTime(), 10, "Simple excursion 1", "New York", 35);
ex1 = baseDao.persist(ex1);
assertEquals(baseDao.findById(ex1.getId()), ex1);
}
@Test
public void testPersist() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
String dbURL = "jdbc:derby:memory:TestingDB;create=true";
Connection conn = getConnection(dbURL);
EntityManagerFactory f = Persistence.createEntityManagerFactory("TestingDB");
EntityManager em = f.createEntityManager();
BaseDAOImpl b = new BaseDAOImpl(Excursion.class, em);
Calendar c = Calendar.getInstance();
c.set(2013, 8, 24);
Excursion ex1 = createExcursion(c.getTime(), 10, "Simple excursion 1", "New York", 35);
ex1 = b.persist(ex1);
assertEquals(b.findById(ex1.getId()), ex1);
} catch (ClassNotFoundException ex) {
Logger.getLogger(BaseDAOImplTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
testPersist
正在运行,结果是此测试通过了。但是在testPersistExcursion
中,我在NullPointerException
时收到错误,因为在setUp
我得到了:
java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
我只是想将EntityManager
添加为全局并在setUp
初始化,所以我不需要在每个单元测试中再次编写这些行(我在一些文章中看到它并且我认为它很好的形式给出)。但这有什么问题呢?为什么它在单元测试中工作但在setUp
我找不到那个驱动程序?感谢
修改
我发现问题,那是在maven。我有错误的artifactId。但是现在我在"Unable to build EntityManagerFactory"
<{1}}处遇到问题:emFactory = Persistence.createEntityManagerFactory("TestingDB");
我的持久单位:
<persistence-unit name="TestingDB" transaction-type="RESOURCE_LOCAL">
<class>cz.infi.javatravelagency.entities.Reservation</class>
<class>cz.infi.javatravelagency.entities.Trip</class>
<class>cz.infi.javatravelagency.entities.Customer</class>
<class>cz.infi.javatravelagency.entities.Excursion</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.connection.username" value="APP"/>
<property name="hibernate.connection.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="hibernate.connection.password" value="APP"/>
<property name="hibernate.connection.url" value="jdbc:derby:memory:TestingDB;create=true"/>
<property name="hibernate.connection.autoReconnect" value="true" />
<property name="hibernate.connection.autoReconnectForPools" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
</properties>
</persistence-unit>
答案 0 :(得分:0)
好的,继续我的评论,我已经尝试了这个,它按预期工作。运行测试时,src / test / resources / META-INF中的测试特定persistence.xml将按预期获取。
然后,您可以创建一个BaseDao类,其长度如下:
public class BaseDao {
private static EntityManagerFactory emf;
public static EntityManager getEntityManager() {
if (emf == null) {
emf = Persistence.createEntityManagerFactory("test");
}
return emf.createEntityManager();
}
}
}
哪个好多了!