通用 DAO 类的通用测试类?
我正在尝试测试一些从通用 DAO 继承的 DAO 类,并且想知道是否有一种方法可以编写一个通用测试类来测试我的通用 DAO 的功能,并创建从它继承的其他测试类
我的通用 DAO类:
public abstract class GenericDAO<T> {
private final Class<T> entityClass;
public GenericDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
//bunch of methods
//...
}
一个示例 DAO,继承自我
public class OptionsDAO extends GenericDAO<Options> {
public OptionsDAO() {
super(Options.class);
}
}
当前测试中的通用 DAO,
public class OptionsDAOTest {
//...
@Test
public void testSomeMethod() {
OptionsDAO odao = new OptionsDAO(); //this is what blocked me from achieving what i'm trying to do (look @next sction)
odao.callSomeMethod();
//Asserts and what not...
}
//...
}
这就是我希望做的
public class GenericDAOTest<T> {
private final Class<T> entityClass;
public GenericDAOTest(Class<T> entityClass) {
this.entityClass = entityClass;
}
@Test
public void testSomeMethod() {
GenericDAO gdao = ???//how can i get an instance based on the T passed to the class?
odao.callSomeMethod();
//Asserts and what not...
}
//...
}
public class OptionsDAOTest extends GnericDAOTest<Options> {
public OptionsDAOTest() {
super(OptionsDAO.class);
}
//how can i call inhrited methods here to test them on an Options java bean
//and do the same with oter DAO classes i have?
}
I'm trying to test some DAO Classes that inherit from a Generic one, and was wondering if there was a way to write a generic test class that tests the functionality of my generic DAO, and create other test classes inheriting from it
My Generic DAO Class :
public abstract class GenericDAO<T> {
private final Class<T> entityClass;
public GenericDAO(Class<T> entityClass) {
this.entityClass = entityClass;
}
//bunch of methods
//...
}
a sample DAO inheriting from my generic one
public class OptionsDAO extends GenericDAO<Options> {
public OptionsDAO() {
super(Options.class);
}
}
what i have currently in tests
public class OptionsDAOTest {
//...
@Test
public void testSomeMethod() {
OptionsDAO odao = new OptionsDAO(); //this is what blocked me from achieving what i'm trying to do (look @next sction)
odao.callSomeMethod();
//Asserts and what not...
}
//...
}
THIS is what I'm hoping to do
public class GenericDAOTest<T> {
private final Class<T> entityClass;
public GenericDAOTest(Class<T> entityClass) {
this.entityClass = entityClass;
}
@Test
public void testSomeMethod() {
GenericDAO gdao = ???//how can i get an instance based on the T passed to the class?
odao.callSomeMethod();
//Asserts and what not...
}
//...
}
public class OptionsDAOTest extends GnericDAOTest<Options> {
public OptionsDAOTest() {
super(OptionsDAO.class);
}
//how can i call inhrited methods here to test them on an Options java bean
//and do the same with oter DAO classes i have?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果泛型类提供无参数构造函数,您只需调用
entityClass.newInstance()
即可。请注意,
entityClass
的类型为Class
类型,因此属于T
类型。You can just call
entityClass.newInstance()
if the generic class provides a no-arg constructor.Note that
entityClass
is of typeClass<T>
and thus of typeT
.