gpt4 book ai didi

java - 使用内存中的 derby 对数据库层 DAO 进行单元测试

转载 作者:行者123 更新时间:2023-12-01 04:16:11 26 4
gpt4 key购买 nike

我想用这个来单元测试我的 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。但现在我在这一行遇到了 “无法构建 EntityManagerFactory” 的问题: 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>

最佳答案

好的,根据我的评论,我已经尝试过这个,并且它按预期工作。运行测试时,会按预期选取 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();
}
}

}

这一切都好得多!

关于java - 使用内存中的 derby 对数据库层 DAO 进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19410646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com