通用 DAO 类的通用测试类?

发布于 2024-12-02 01:24:48 字数 1584 浏览 0 评论 0原文

我正在尝试测试一些从通用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光磨忆 2024-12-09 01:24:48

GenericDAO gdao = ???//我如何根据传递给类的 T 获取实例?

如果泛型类提供无参数构造函数,您只需调用 entityClass.newInstance() 即可。
请注意,entityClass 的类型为 Class 类型,因此属于 T 类型。

GenericDAO gdao = ???//how can i get an instance based on the T passed to the class?

You can just call entityClass.newInstance() if the generic class provides a no-arg constructor.
Note that entityClass is of type Class<T> and thus of type T.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文