0
我正在使用Spring爲我的Hibernate DAO類編寫集成測試。我使用MySQL進行開發,H2使用我的內存數據庫進行集成測試。我使用hibernate.hbm2ddl.import_files
導入MySQL數據庫的模式。一切正常,但我的斷言失敗了。Hibernate集成測試失敗
問題是,當我運行應用程序時,DAO類工作,所以我知道我的測試存在問題。我認爲我的問題是當休眠導出模式時數據沒有被插入。我怎樣才能解決這個問題?
測試類:
@RunWith (SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations="/spring/mock-application-config.xml")
public class DAOTest{
@Autowired
private Dao dao;
public VideoDao getDao()
{
return dao;
}
public void setDao(Dao dao)
{
this.dao = dao;
}
@Test
public void findById()
{
Video video = dao.findById("someId");
Assert.assertNotNull(video);
}
@Test
public void findAll()
{
List<Video> videos = dao.findAll(1, 10);
Assert.assertNotNull(videos);
Assert.assertFalse(videos.isEmpty());//this assertion fails
}
}
模擬應用-config.xml中:
<!-- Mock DataSource -->
<bean id="mockdataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="username" value="root"/>
<property name="url" value="jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE"/>
</bean>
<!-- Mock Session Factory -->
<bean id="mockSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="mockdataSource"/>
<property name="packagesToScan" value="com.videovix.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.import_files">database.sql</prop>
</props>
</property>
</bean>
<bean id="dao" class="com.videovix.dao.DaoHibernate"/>
<bean id="videoService" class="com.videovix.util.service.VideoServiceImpl"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="mockdataSource"/>
<property name="sessionFactory" ref="mockSessionFactory"/>
</bean>
database.sql:
CREATE DATABASE IF NOT EXISTS `database` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `videovix`;
-- --------------------------------------------------------
--
-- Table structure for table `artist`
--
CREATE TABLE IF NOT EXISTS `artist` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;
--
-- Dumping data for table `artist`
--
INSERT INTO `artist` (`id`, `name`) VALUES
(1, 'Miley Cyrus'),
(3, 'Katy Perry'),
(4, 'Imagine Dragons'),
(5, 'Justin Timberlake'),
(6, 'Selena Gomez'),
(7, 'Macklemore & Ryan Lewis'),
(8, 'Naughty Boy'),
(11, 'One Direction'),
(12, 'Bruno Mars'),
(13, 'Rihanna');
-- --------------------------------------------------------
--
-- Table structure for table `artist_video`
--
CREATE TABLE IF NOT EXISTS `artist_video` (
`Artist_id` bigint(20) NOT NULL,
`videos_id` varchar(255) NOT NULL,
UNIQUE KEY `UK_dd1aa30e7fe34c58832c64e0624` (`videos_id`),
UNIQUE KEY `UK_4a52607ddfcc468f8749c77412f` (`videos_id`),
UNIQUE KEY `UK_e1b263d89a864001a33cd027269` (`videos_id`),
UNIQUE KEY `UK_753c2be39637480281cab765151` (`videos_id`),
UNIQUE KEY `UK_31df9ea6edd64fe99cb51c05bc5` (`videos_id`),
UNIQUE KEY `UK_cfdba9990220431c99dd13c0e20` (`videos_id`),
UNIQUE KEY `UK_4eafcbec6e2a4909adcae83e08e` (`videos_id`),
UNIQUE KEY `UK_cb784fff2c6148bcb472784d7ec` (`videos_id`),
UNIQUE KEY `UK_97be99a5fb244a23acefe9a9ab0` (`videos_id`),
UNIQUE KEY `UK_f82154e4970147c584765636304` (`videos_id`),
UNIQUE KEY `UK_8c535b222e3c43d48d2b937819b` (`videos_id`),
UNIQUE KEY `UK_483d1d232e5b47f0966602e31c6` (`videos_id`),
UNIQUE KEY `UK_8fabe069cc904218a8262efc5ac` (`videos_id`),
UNIQUE KEY `UK_5764620a13684560884e6346056` (`videos_id`),
UNIQUE KEY `UK_b9eb1dd5fbd9495b8d1f3b30a62` (`videos_id`),
UNIQUE KEY `UK_85eda9a560794f9b8ba79bcb719` (`videos_id`),
UNIQUE KEY `UK_ccd42e63f80c4b82ace5ba0001d` (`videos_id`),
UNIQUE KEY `UK_1c016bc1da37456a8b1648ba48b` (`videos_id`),
UNIQUE KEY `UK_4d4a065aa23349d9a254ccb5398` (`videos_id`),
UNIQUE KEY `UK_2821cf2f681444138f8115a21ab` (`videos_id`),
UNIQUE KEY `UK_5ee099592fcb4782b12ecb111dc` (`videos_id`),
UNIQUE KEY `UK_126cb73e7f45492a9c8c8ef1e55` (`videos_id`),
UNIQUE KEY `UK_fa5ad80ec9e44383ae9cd8f2851` (`videos_id`),
UNIQUE KEY `UK_8b5e303b7c6c431db3a75c0ee10` (`videos_id`),
UNIQUE KEY `UK_d3be1deeee084b1498d872dfc17` (`videos_id`),
UNIQUE KEY `UK_beeab2f23ec74f64a2ba3c795d5` (`videos_id`),
UNIQUE KEY `UK_e5c801f131464ea5a845cc843d4` (`videos_id`),
UNIQUE KEY `UK_792f7c7d1a2f4883bdb5f53be52` (`videos_id`),
UNIQUE KEY `UK_47c72ec806c24eb0b66827cb4e7` (`videos_id`),
UNIQUE KEY `UK_ad9a0a0767b34908bd1508c43a0` (`videos_id`),
UNIQUE KEY `UK_c4812b7dbb5e4d5c8dc3eab95d5` (`videos_id`),
UNIQUE KEY `UK_e2f40d260c8548a19b9ca6a73a4` (`videos_id`),
UNIQUE KEY `UK_0a57f3fcf3214fffb778a950158` (`videos_id`),
UNIQUE KEY `UK_5f4b83ca0f594bf1a31b2b221b1` (`videos_id`),
UNIQUE KEY `UK_fce00fc5ba744c48802aa273048` (`videos_id`),
UNIQUE KEY `UK_66764d25ec6d4fc8a44aba34c20` (`videos_id`),
UNIQUE KEY `UK_4c87e88fd2354e71abd6520e9ff` (`videos_id`),
UNIQUE KEY `UK_8db7ceddb3424b0aaf7f9cb91a6` (`videos_id`),
UNIQUE KEY `UK_2ed029fce45a4377b39f40a798a` (`videos_id`),
UNIQUE KEY `UK_8df810e5daf04b629fa87d8afee` (`videos_id`),
UNIQUE KEY `UK_5018cb289891442eb31d49b26e5` (`videos_id`),
UNIQUE KEY `UK_7c64bd676d4e41079d056961cd4` (`videos_id`),
UNIQUE KEY `UK_7ec6e3f8d3b94948af077851d50` (`videos_id`),
UNIQUE KEY `UK_6427363762f0481daac740ae1df` (`videos_id`),
UNIQUE KEY `UK_ca3326c1e110449b98b6b847afc` (`videos_id`),
UNIQUE KEY `UK_21f6914e17bc4e608d1265c9614` (`videos_id`),
UNIQUE KEY `UK_0569689c2bda4e198e8b66fd333` (`videos_id`),
UNIQUE KEY `UK_dd1948e41a724e8192cb23aa596` (`videos_id`),
UNIQUE KEY `UK_e2df5e55539b464cac50f878bc9` (`videos_id`),
UNIQUE KEY `UK_a905895f8d494ad3ab302a77599` (`videos_id`),
UNIQUE KEY `UK_a18631136dd74f758f73db0bb0d` (`videos_id`),
UNIQUE KEY `UK_522f42dd7f264e4aba872ea4c32` (`videos_id`),
UNIQUE KEY `UK_62de3bb2fdee477392e8f2d6d17` (`videos_id`),
UNIQUE KEY `UK_1e28c47a58174ce5b00913a405b` (`videos_id`),
UNIQUE KEY `UK_8d0278620ef340ae837b427aef2` (`videos_id`),
UNIQUE KEY `UK_22a4be66cd3c4c6183978523c59` (`videos_id`),
UNIQUE KEY `UK_3fee230efa39416e8a4f72e7e09` (`videos_id`),
UNIQUE KEY `UK_305a8b23d83f4b6e9c642173bf1` (`videos_id`),
UNIQUE KEY `UK_3341b3a4d3284de9b553f7fc393` (`videos_id`),
UNIQUE KEY `UK_3103ebf20d0847cf807630362ea` (`videos_id`),
UNIQUE KEY `UK_989daef791014e148df37e074e0` (`videos_id`),
UNIQUE KEY `UK_e2b94c38ee6e406e898841ba530` (`videos_id`),
KEY `FK_066a90f961c34a0ca1083d72fcc` (`videos_id`),
KEY `FK_a60dbbcdf7914df7a8615e70099` (`Artist_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `artist_video`
--
INSERT INTO `artist_video` (`Artist_id`, `videos_id`) VALUES
(1, 'LrUvu1mlWco'),
(1, 'My2FRPA3Gf8'),
(3, 'CevxZvSJLk8'),
(4, 'mWRsgZuwf_8'),
(5, 'uuZE_IRwLNI'),
(6, 'n-D1EB74Ckg'),
(7, '2zNSgSzhBfM'),
(8, '3O1_3zBUKM8'),
(11, 'o_v9MY_FMcw'),
(12, 'ekzHIouo8Q4'),
(13, 'ehcVomMexkY');
-- --------------------------------------------------------
--
-- Table structure for table `video`
--
CREATE TABLE IF NOT EXISTS `video` (
`id` varchar(255) NOT NULL,
`image` varchar(255) DEFAULT NULL,
`name` varchar(255) DEFAULT NULL,
`artist_id` bigint(20) DEFAULT NULL,
`view_id` bigint(20) DEFAULT NULL,
`genre` varchar(255) DEFAULT NULL,
`defaultImage` varchar(255) DEFAULT NULL,
`highImage` varchar(255) DEFAULT NULL,
`mediumImage` varchar(255) DEFAULT NULL,
`viewCount` int(11) NOT NULL,
`year` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_1926bd9e86644f088a4fac2a26d` (`artist_id`),
KEY `FK_247629b656454444bea64dee5f7` (`view_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `video`
--
INSERT INTO `video` (`id`, `image`, `name`, `artist_id`, `view_id`, `genre`, `defaultImage`, `highImage`, `mediumImage`, `viewCount`, `year`) VALUES
('2zNSgSzhBfM', '//i1.ytimg.com/vi/2zNSgSzhBfM/mqdefault.jpg', ' Can''t Hold Us Feat Ray Dalton', 7, NULL, 'hip-hop', NULL, NULL, NULL, 0, 0),
('3O1_3zBUKM8', 'http://i3.ytimg.com/vi/3O1_3zBUKM8/mqdefault.jpg', 'La La La ft. Sam Smith', 8, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('CevxZvSJLk8', '//i1.ytimg.com/vi/CevxZvSJLk8/mqdefault.jpg', 'Roar', 3, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('ehcVomMexkY', '//i1.ytimg.com/vi/ehcVomMexkY/mqdefault.jpg', 'Pour It Up', 13, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('ekzHIouo8Q4', 'http://i3.ytimg.com/vi/ekzHIouo8Q4/mqdefault.jpg', 'When I Was You Man', 12, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('LrUvu1mlWco', '//i1.ytimg.com/vi/LrUvu1mlWco/mqdefault.jpg', 'We Can''t Stop', 1, NULL, NULL, NULL, NULL, NULL, 0, 0),
('mWRsgZuwf_8', '//i1.ytimg.com/vi/mWRsgZuwf_8/mqdefault.jpg', 'Demons', 4, NULL, 'rock', NULL, NULL, NULL, 0, 0),
('My2FRPA3Gf8', '//i1.ytimg.com/vi/My2FRPA3Gf8/mqdefault.jpg', 'Wrecking Ball', 1, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('n-D1EB74Ckg', '//i1.ytimg.com/vi/n-D1EB74Ckg/mqdefault.jpg', 'Come & Get It', 6, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('o_v9MY_FMcw', 'http://i3.ytimg.com/vi/o_v9MY_FMcw/mqdefault.jpg', 'Best Song Ever', 11, NULL, 'pop', NULL, NULL, NULL, 0, 0),
('uuZE_IRwLNI', '//i1.ytimg.com/vi/uuZE_IRwLNI/mqdefault.jpg', 'Mirrors', 5, NULL, 'pop', NULL, NULL, NULL, 0, 0);
-- --------------------------------------------------------
--
-- Table structure for table `view`
--
CREATE TABLE IF NOT EXISTS `view` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`genre` varchar(255) DEFAULT NULL,
`viewed` bigint(20) NOT NULL,
`video_id` varchar(255) DEFAULT NULL,
`date` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_74ad9d0cce3d49dcaa0e7b72daa` (`video_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `artist_video`
--
ALTER TABLE `artist_video`
ADD CONSTRAINT `FK_066a90f961c34a0ca1083d72fcc` FOREIGN KEY (`videos_id`) REFERENCES `video` (`id`),
ADD CONSTRAINT `FK_a60dbbcdf7914df7a8615e70099` FOREIGN KEY (`Artist_id`) REFERENCES `artist` (`id`);
--
-- Constraints for table `video`
--
ALTER TABLE `video`
ADD CONSTRAINT `FK_1926bd9e86644f088a4fac2a26d` FOREIGN KEY (`artist_id`) REFERENCES `artist` (`id`),
ADD CONSTRAINT `FK_247629b656454444bea64dee5f7` FOREIGN KEY (`view_id`) REFERENCES `view` (`id`);
--
-- Constraints for table `view`
--
ALTER TABLE `view`
ADD CONSTRAINT `FK_74ad9d0cce3d49dcaa0e7b72daa` FOREIGN KEY (`video_id`) REFERENCES `video` (`id`);
對不起,再次通讀這個問題......刪除以前的評論。 –