gpt4 book ai didi

hibernate - 如何在 Tapestry 相关项目中测试 DAO 层

转载 作者:行者123 更新时间:2023-12-02 11:26:44 25 4
gpt4 key购买 nike

我有一个 Tapestry5 + Hibernate 应用程序,我正在尝试为其编写单元测试。我正在寻找一些指导或最佳实践来测试此类项目中的 DAO 层。我使用 testng 尝试了下面的测试用例,但没有成功。

@Test
public void createOrderTest() {
IOCUtilities.buildDefaultRegistry();
OrderManager orderManager = new OrderManager();
Order o1 = new Order();
Date date = new Date(System.currentTimeMillis());
o1.setOrderDate(date);
o1.setOrderStatus(OrderStatus.BILLED.toString());
orderManager.createOrder(o1);
}

当我尝试运行测试用例时收到错误消息。

java.lang.IllegalArgumentException: Contribution org.apache.tapestry5.yuicompressor.services.YuiCompressorModule.contributeMinimizers(MappedConfiguration) (at YuiCompressorModule.java:42) is for service 'interface org.apache.tapestry5.services.assets.ResourceMinimizer' qualified with marker annotations [interface org.apache.tapestry5.ioc.annotations.Primary], which does not exist.
at org.apache.tapestry5.ioc.internal.RegistryImpl.validateContributeDefs(RegistryImpl.java:246)
at org.apache.tapestry5.ioc.internal.RegistryImpl.<init>(RegistryImpl.java:205)
at org.apache.tapestry5.ioc.RegistryBuilder.build(RegistryBuilder.java:177)
at org.apache.tapestry5.ioc.IOCUtilities.buildDefaultRegistry(IOCUtilities.java:51)
at com.vc.xpressor.testng.OrderManagerTest.createOrderTest(OrderManagerTest.java:21)

最佳答案

如果您在 DAO 中使用构造函数注入(inject),则在 DAO 测试中根本不需要 Tapestry。我是否正确,您的 DAO 所需的唯一依赖项是 hibernate session ,也许还有几个@Symbols?

如果是这样,您所需要做的就是设置与内存数据库(例如 h2)的连接并将其包装在 hibernate session 中。然后将 hibernate session 传递给 DAO 构造函数。

我为我的 DAO 使用一个基本测试类,它在 @Before 方法中设置连接和 session

这是一个示例 DAO

public class ItemDAOImpl implements ItemDAO {
private final Session session;

public ItemDAOImpl(Session session) {
this.session = session;
}

public List<Item> findAll() {
return session.createCriteria(Item.class).list();
}

....
}

在 Tapestry 中,这是在 AppModule 中声明的

public static void bind(ServiceBinder binder) {
binder.bind(ItemDAO.class, ItemDAOImpl.class);
}

但在我的测试用例中,我不需要 Tapestry

public class ItemDAOImplTest extends AbstractHibernateTest {
private ItemDAO itemDAO;

@Override
protected void before() {
itemDAO = new ItemDAOImpl(session);
}

@Override
protected void after() {}

@Test
public void testFindAll() {
session.save(new Item(...));
session.save(new Item(...));
session.save(new Item(...));
assertEquals(3, itemDAO.findAll().size());
}
}

public abstract class AbstractHibernateTest {
protected SessionFactory sessionFactory;
protected Session session;

@Before
public void abstractBefore() throws Exception {
Configuration config = new Configuration();

// see PackageNameHibernateConfigurer source code to dynamically add all
// classes in the entities package
config.addAnnotatedClass(Item.class);
config.addAnnotatedClass(...);

config.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
config.setProperty("hibernate.connection.driver_class", "org.h2.Driver");
config.setProperty("hibernate.connection.url", "jdbc:h2:mem:test");
config.setProperty("hibernate.hbm2ddl.auto", "create");
config.setProperty("hibernate.show_sql", "true");
config.setProperty("hibernate.format_sql", "true");

sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
session.beginTransaction();
before();
}

protected abstract void before() throws Exception;

@After
public void abstractAfter() throws Exception {
Exception exception = null;
try {
after();
} catch (Exception e) {
exception = e;
}

session.getTransaction().rollback();
session.close();
sessionFactory.close();

if (exception != null) {
throw exception;
}
}

protected abstract void after() throws Exception;
}

关于hibernate - 如何在 Tapestry 相关项目中测试 DAO 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15664815/

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