如何进行 JUnit 测试来测试 NamedQueries?
我想要一个失败的语法错误,例如:
@Entity
@NamedQueries({
@NamedQuery(name="Country.findByName",
query="SELECT c FROM Country c WHERE c.wrongname = :name")
})
public class Country {
private String name;
//...
}
测试应该失败,因为 name < > wrongname
我只需要语法检查,不需要数据库检查
运行时的 StackTrace:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: unit] Unable to build EntityManagerFactory
...
Caused by: org.hibernate.HibernateException: Errors in named queries: Country.findByName
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:397)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)