springmvc3.2+spring+hibernate4全注解方式整合(四)

以上是工程文件,下面开始测试

 

package test.testservice;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.fangjian.core.platform.po.User;
import com.fangjian.core.platform.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration({"classpath:com/config/spring/spring-common.xml","classpath:com/config/spring/spring-jdbc.xml"})
public class UserServiceTest extends AbstractJUnit4SpringContextTests {
    
    @Autowired
    private UserService userService;
    
    @Test
    public void testSaveUser(){
        User u = new User();
        u.setName("fangjian");
        u.setPassword("fangjian");
        u.setUsername("username");
        
        userService.saveUser(u);
    }
}

测试成功,控制台打印输出

Hibernate: 
    insert 
    into
        IEMS_USER
        (name, password, username, id) 
    values
        (?, ?, ?, ?)

如果要测试事务,修改service实现类代码

package com.fangjian.core.platform.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fangjian.core.platform.dao.UserDao;
import com.fangjian.core.platform.po.User;
import com.fangjian.core.platform.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserDao userdao;
    
    @Override
    public void saveUser(User user) {
        this.userdao.saveUser(user);
        System.out.println(1/0);
        this.userdao.saveUser(user);
    }

}

再次测试,数据库没有信息,junit提示/by zero 错误

 

springmvc+spring+hibernate4基本框架整合完成。

 

posted @ 2014-07-02 14:56  方坚.cn  阅读(971)  评论(0编辑  收藏  举报