我有mysql db,我从中生成了hibernate实体,现在我需要从这些实体生成内存数据库进行测试。我在尝试运行单元测试时遇到此错误。
/ *** main] o.h.engine.jdbc.spi.SqlExceptionHelper:SQL错误:42102,SQLState:42S02 2016-02-16 18:10:47.864 ERROR 29758 --- [main] o.h.engine.jdbc.spi.SqlExceptionHelper:Table" tbl_all_orders"未找到; SQL语句: ** /
看起来db创建失败了。
这是我的测试属性文件内容:
db.driver: org.h2.Driver
db.url: jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false
db.username: sa
db.password:
hibernate.dialect: org.hibernate.dialect.H2Dialect
hibernate.show_sql: true
hibernate.format_sql: true
hibernate.hbm2ddl.auto: create
hibernate.archive.autodetection=class, hbm
entitymanager.packagesToScan: linda
答案 0 :(得分:0)
这是我测试h2的例子。您可以稍微改变一下,看看它适合您的情况。主要是您需要手动创建db表并让config.xml包含您的数据库。 您可以手动创建.sql文件并创建表,如果使用spring,则让bean包含它。
someTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("testConfig.xml") // <-- this xml you need to include
public class PortDaoImplTest {
private static final Logger log = Logger.getLogger(sysInfoDaoImplTest.class);
@Autowired
private sysInfoDaoImpl sysDao;
@After
public void tearDown(){
portDao = null;
}
@Test
public void testGetPort() {
log.info("Testing getInfo(String id)...");
SysInfo p = sysDao.getInfo("nysdin2039");
assertNotNull(p);
}
testConfig.xml
...xml header...
<!-- h2 driver -->
<bean id="test.dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="false" >
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:mem:test_mem;DB_CLOSE_DELAY=-1;MODE=ORACLE" />
</bean>
<!-- datasource file -->
<jdbc:initialize-database data-source="test.dataSource">
<!-- table list -->
<jdbc:script location="com/yourPath/h2/schema.sql" />
<jdbc:script location="com/yourPath/h2/test_data.sql" />
</jdbc:initialize-database>
<!-- bean def -->
<bean id="sysInfoDao" class="com.mycompanyName.sysInfoDaoImpl" >
<property name="log" ref="test.log" />
<property name="dataSource" ref="test.dataSource" />
</bean>
....
h2 schema.sql文件
drop table IF EXISTS tbl_all_orders;
CREATE TABLE tbl_all_orders
(
your_stuff_ID NUMBER NOT NULL,
your_other_column_stuff VARCHAR2(15) DEFAULT
);
...相应地添加约束或列......
test_data.sql文件
INSERT into tbl_all_orders
(your_column_names... , your_other_column_names...)
VALUES
(1, 'values',...);
答案 1 :(得分:0)
这是我的测试工作配置(src / test / resources文件夹中的database.properties)
# DB properties
db.driver=org.h2.Driver
db.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
db.username=sa
db.password=
# Hibernate Configuration
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
# validate: validate the schema, makes no changes to the database.
# update: update the schema.
# create: creates the schema, destroying previous data.
# create-drop: drop the schema at the end of the session.
hibernate.hbm2ddl.auto=create
entitymanager.packages.to.scan=abcde
顺便说一句,你的单元测试不应该在数据库上。