【发布时间】:2015-07-09 00:42:44
【问题描述】:
我正在尝试为使用 Hibernate/JPA 实体和 DAO 的基于 Spring Boot 的应用程序编写单元测试。以下是我到目前为止所遵循的步骤:
1) 在 pom.xml 中添加以下内容
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
2) 在 ../test/resources/application.properties 中,我添加了这个:
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.database = HSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect
spring.datasource.driverClassName = org.hsqldb.jdbcDriver
spring.datasource.url: jdbc:hsqldb:mem:scratchdb
spring.datasource.username = sa
spring.datasource.password =
3) 在 ../test/resources/import.sql 中,我添加了一些“插入...”数据创建脚本。
insert into groups(GROUP_NAME, THREAD_POOL_SIZE) values ("TEST GROUP 1", 5);
4) 单元测试如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class TestGroupDao {
@Autowired
GroupDao groupDao;
@Test
public void testFindByName() {
Group group = groupDao.findByName("TEST GROUP 1");
//assertThat(group.getPoolSize(), is(equalTo(5)));
}
}
当我运行此测试时,我收到错误消息,例如:
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: alter table..
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: user lacks privilege or object not found: PUBLIC.GROUP
5) 集团实体:
@Entity
@javax.persistence.Table(name = "groups", uniqueConstraints = {
@UniqueConstraint(columnNames = "GROUP_NAME"),
})
public class Group {
// ==============
// PRIVATE FIELDS
// ==============
// An autogenerated id (unique for each group in the db)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "GROUP_ID", unique = true, nullable = false)
private long id;
@Column(name = "GROUP_NAME", unique = true, nullable = false)
private String name;
我错过了什么?
【问题讨论】:
-
发布您的组实体映射和应用程序配置的相关部分。
-
我尝试将 import.sql 重命名为 data.sql 并创建 schema.sql 来创建组表,但仍然出现此错误。
-
不完全确定我改变了什么,但这现在可以工作了。我唯一做的就是将所有必要的 SQL 添加到 import.sql。可能就是这个原因。
-
你有这些字段的setter和getter吗?
标签: spring hibernate unit-testing jpa spring-boot