开发者

Data model testing with Hibernate

In my project i have a lot of hbm.xml files from which i generate the java classes and the sql for the db. Semantic errors within the hbm.xml files are shown to me after calling buildSessionFactory() which is really annoying for the current situation. I would like to have a test class which does that for me with a slightly different config(use ebedded derby instead). My current "solution" looks like that:

    String dbName = "test";

    try{
        SessionFactory fact = new Configuration().configure()
                                                 .setProperty(开发者_如何转开发"hibernate.connection.driver_class","org.apache.derby.jdbc.EmbeddedDriver")
                                                 .setProperty("hibernate.connection.url","jdbc:derby:" + dbName + ";create=true")
                                                 .setProperty("hibernate.connection.username","")
                                                 .setProperty("hibernate.connection.password","")
                                                 .setProperty("hibernate.dialect","org.hibernate.dialect.DerbyDialect")
                                                 .setProperty("hibernate.hbm2ddl.auto","create-drop")
                                                 .buildSessionFactory();
        assertNotNull(fact);

        Session s = fact.openSession();
        assertNotNull(s);

        s.close();
        fact.close();
    }catch(Throwable t){
        fail(t.getMessage());
    }

But this it not really satisfying from my point of view. I would also like to check if the named queries and so on are all ok. Is there a way to do that kind of tests in an automated process?

The second part of my question then is, if derby is a good choice for testing, why can't i automatically delete the db after the test ended? I'd like to specify a parameter in the url like

    "jdbc:derby:" + dbName + ";create=true;drop=true"

By using this solution i end up with a dir named like the variable dbName and a derby file in my project directory which is not cool.


If you want to test your named queries, a solution could be to call them in the test with bogus arguments (the queries would probably return nothing, but at least it would force hibernate to check them) ; something along the lines of :

 s.getNamedQuery("Whatever").setParameters(...).execute/query

Now is your problem the fact that you cannot easily get the names of all queries ?

As for the derby db, can't you just "manually" get rid of the created folder at the end of your test (in the tearDown, for example ?)

It seems like the location of where the derby base is created can be controled with a java property :

http://db.apache.org/derby/manuals/develop/develop14.html

http://db.apache.org/derby/manuals/develop/develop12.html#HDRSII-DEVELOP-13018

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜