我有一个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)
答案 0 :(得分:5)
如果在DAO中使用构造函数注入,则在DAO测试中根本不需要tapestry。我是否正确你的DAO所需的唯一依赖是hibernate会话,也许是几个@Symbols?
如果是这样,您只需要建立与内存数据库(例如h2)的连接并将其包装在休眠会话中。然后将hibernate会话传递给DAO构造函数。
我为我的DAO使用基础测试类,它在@Before方法中设置连接和会话
这是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;
}
答案 1 :(得分:3)
另一个选择,如果你想通过IoC运行DAO,就是将AppModule分成两个:一个模块用于定义Hibernate实体和DAO,其余模块用于AppModule。您可以使用DAOModule和HibernateCoreModule初始化您的测试(如果您注意到,Tapestry的Hibernate支持同样分为两部分:一个在tapestry-ioc之上工作的非可视部分,以及一个为Tapestry提供额外钩子的可视部分 - 芯)。