论坛首页 Java版 Spring

Spring下的业务层Unit Test修正版

浏览 2893 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2005-03-23
这篇教程很好推荐一下。

http://dev.csdn.net/article/62/62196.shtm
顺便问一下.他提到的

引用
import com.itorgan.util.DbUtil;


这个包我怎么也找不到.哪位 能告诉我下.怎么能得到阿 ?谢谢
   
最后更新时间:2005-03-24
这是他自己定义的包吧
问问他本人

calvin怎么会写了这么简单的一个教程
难道是教用JUnit的吗
   
0 请登录后投票
最后更新时间:2005-03-29
flyingbug 写道
这是他自己定义的包吧
问问他本人

calvin怎么会写了这么简单的一个教程
难道是教用JUnit的吗 8)



简单 ???  QQ : 327278581  能不能加我 。教我一下。
   
0 请登录后投票
最后更新时间:2005-03-29
按照上面那个教程,我自己写了一下。结果测试不通过。代码如下。哪位能帮我看看哪里出错。
引用
/*
* Created on 2005-1-26
*/
package test.erp;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.DatabaseSequenceFilter;
import org.dbunit.dataset.FilteredDataSet;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.filter.ITableFilter;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

import test.DbUtil;


public class DAOTestCase extends AbstractDependencyInjectionSpringContextTests{
    protected ArrayList contexts = null;   
    protected DatabaseConnection dbunitConn = null; //DBUnit相关属性      
    protected boolean needBackup = false;   //是否需要测试前备份     */  
    protected boolean needRestore = false;   //是否需要在测试后重新载入刚才备份的数据
    protected String xmlFilePath = null;  //备份文件的路径,     * 如果为空,默认为test/dbunitData/myTestData.xml/
    protected String[] tableNames = {"EMPLOYEE_INF"}; // 需要备份的数据库Table名
   
    public DAOTestCase(){        //设了这个,就能autowire by name,否则by setter.       
    setPopulateProtectedVariables(true);
        contexts = new ArrayList();       
contexts.add("/applicationContext-hibernate.xml");       
contexts.add("/applicationContext-resources.xml");
contexts.add("/applicationContext-service.xml"); 
}
    public String[] getConfigLocations(){       
    String[] tmp = new String[contexts.size()];       
    return (String[]) contexts.toArray(tmp);   
}
    public void onSetUp() throws Exception{     
    if (needBackup){           
    DbUtil dbUtil = new DbUtil(applicationContext);           
    dbunitConn = new DatabaseConnection(dbUtil.getConn(), "ZX0111");           
    if (xmlFilePath == null)               
    xmlFilePath = "myTestData.xml";
    if (tableNames != null){               
    ITableFilter filter = new DatabaseSequenceFilter(dbunitConn, tableNames);              
    IDataSet dataset = new FilteredDataSet(filter, dbunitConn.createDataSet());
        File f_file = new File(xmlFilePath);               
        new File(f_file.getParent()).mkdirs();              
        XmlDataSet.write(dataset, new FileOutputStream(f_file));
    }          
    else{              
    throw new Exception("your choice backup data but no table names asign");
    }
        }   
    }
    public void onTearDown() throws Exception{
        if (needRestore){          
        if (dbunitConn == null){               
        DbUtil dbUtil = new DbUtil(applicationContext);             
        dbunitConn = new DatabaseConnection(dbUtil.getConn(),"ZX0111");           
        }  
        IDataSet dataSet = new XmlDataSet(new FileInputStream(xmlFilePath));          
        DatabaseOperation.REFRESH.execute(dbunitConn, dataSet);     
        }       
        dbunitConn.close();
    }
    protected final static ApplicationContext ctx;
    static {
        // the dao.type is written to the database.properties file
        // in properties.xml
        //ResourceBundle db = ResourceBundle.getBundle("database");
        //String daoType = db.getString("dao.type");
   
        String BASE_DIRECTORY = "G:/erp/Web"; //项目基准路径
        // spring基本配置文件相对路径
        String[] paths = new String[]{BASE_DIRECTORY + "/WEB-INF/applicationContext-hibernate.xml",BASE_DIRECTORY + "/WEB-INF/applicationContext-service.xml",BASE_DIRECTORY + "/WEB-INF/applicationContext-resources.xml"};
        ctx = new FileSystemXmlApplicationContext(paths);
    }
}



引用
/*
* Created on 2005-1-28
*/
package test.erp;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.erp.po.EmployeeInf;

import com.erp.dao.DAO;
import com.erp.dao.ICompanyInfDAO;
import com.erp.dao.IEmployeeDAO;

/**
* @author 张欣
*/
public class TestEmployeeDAO extends DAOTestCase{   
    private EmployeeInf employeeInf = null;
    protected DAO dao;
    protected IEmployeeDAO employeeDAO;
    protected ICompanyInfDAO companyInfDAO;

    public void onSetUp() throws Exception{
        super.onSetUp();
    }
   
   
    public void testGetEmployee() throws Exception {
    employeeInf = new EmployeeInf();
    employeeInf.setEmployeeName("张三");
    employeeInf.setEmail("zz@11.com");

    dao.saveObject(employeeInf);   
        assertNotNull(employeeInf.getEmployeeId());
       
        employeeInf =(EmployeeInf)dao.getObject(EmployeeInf.class,employeeInf.getEmployeeId());
        assertEquals(employeeInf.getEmployeeName(), "张三");
    }
   
    public static Test suite(){       
    return new TestSuite(TestEmployeeDAO.class);   
    }
}


引用

/*
* Created on 2005-1-29
*/
package test;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;

/**
* @author 张欣
*/
public class DbUtil {
DataSource dataSource;
public DbUtil(ApplicationContext applicationContext ){
dataSource=(DataSource)applicationContext .getBean("dataSource");
}
public Connection getConn() throws SQLException{
return dataSource.getConnection();
}
}


现在问题是 : 点测试:
引用
java.lang.IllegalAccessException: Class org.springframework.test.AbstractDependencyInjectionSpringContextTests can not access a member of class test.erp.TestEmployeeDAO with modifiers "protected"
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:57)
at java.lang.reflect.Field.doSecurityCheck(Field.java:811)
at java.lang.reflect.Field.getFieldAccessor(Field.java:758)
at java.lang.reflect.Field.set(Field.java:519)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.populateProtectedVariables(AbstractDependencyInjectionSpringContextTests.java:184)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:115)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)

出现这个错误。 如果 把
protected DAO dao;改成public则错误更严重。
[code:1]ERROR [main] (CommonsLoggingLogSystem.java:42) - ResourceManager : unable to find resource 'VM_global_library.vm' in any resource loader.
ERROR [main] (CommonsLoggingLogSystem.java:42) - ResourceManager : unable to find resource 'VM_global_library.vm' in any resource loader.


WARN [main] (AbstractDependencyInjectionSpringContextTests.java:194) - No bean with name 'dbunitConn'
WARN [main] (AbstractDependencyInjectionSpringContextTests.java:194) - No bean with name 'xmlFilePath'
WARN [main] (AbstractDependencyInjectionSpringContextTests.java:194) - No bean with name 'managedVariableNames'
ERROR [main] (AbstractDependencyInjectionSpringContextTests.java:232) - onTearDown error
java.lang.NullPointerException
at test.erp.DAOTestCase.onTearDown(DAOTestCase.java:71)
at org.springframework.test.AbstractDependencyInjectionSpringContextTests.tearDown(AbstractDependencyInjectionSpringContextTests.java:229)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)
[/code:1]
   
0 请登录后投票
最后更新时间:2005-03-29
引用
    public DAOTestCase()    {        //设了这个,就能autowire by name,否则by setter.        setPopulateProtectedVariables(true);
        contexts = new ArrayList<String>();        contexts.add("/applicationContext-Hibernate.xml");        contexts.add("/applicationContext-SetupService.xml");    }


//设了这个,就能autowire by name,否则by setter.  这句话是什么意思啊 ?

难道myTestData.xml 里面要写一些东西么?
   
0 请登录后投票
最后更新时间:2005-04-01
梅 写道
引用
    public DAOTestCase()    {        //设了这个,就能autowire by name,否则by setter.        setPopulateProtectedVariables(true);
        contexts = new ArrayList<String>();        contexts.add("/applicationContext-Hibernate.xml");        contexts.add("/applicationContext-SetupService.xml");    }


//设了这个,就能autowire by name,否则by setter.  这句话是什么意思啊 ?

难道myTestData.xml 里面要写一些东西么?


那是因为该函数名与作者在配置文件中bean的名字一样,通过autowire="byName"自动绑定了
   
0 请登录后投票
论坛首页 Java版 Spring

跳转论坛:
JavaEye推荐