You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by francesco77 <f....@reply.it> on 2007/04/20 13:30:22 UTC

JUnit Test with IBatis

Hi to All,

I'd like to write a TestCase using JUnit to test my DAO.
This DAO works correctly using the WEB Application, but I'd like write a
"standalone" test case for the DAO.

So I write this java class :

*****************************************************************

package com.fiat.mida.controller.dashboard;

import java.io.IOException;
import java.io.Reader;
import java.util.Map;

import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;

import com.fiat.mida.model.dashboard.SearchBean;

import junit.framework.TestCase;

import com.fiat.mida.model.dashboard.dao.SearchRefuseDao;
import com.fiat.mida.model.dashboard.logic.*;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class AbstractTest extends TestCase{
	
	private SearchRefuseService service;
	private SearchBean bean;
	private String resource =
"com/fiat/mida/controller/dashboard/test-sql-map-config.xml";
	
	private Reader reader ;
	
                protected DriverManagerDataSource m_dataSource; 
	protected SqlMapClient m_sqlMapClient;
	private SearchRefuseDao m_dao;

	protected void setUp() {
		
		try {
			reader = Resources.getResourceAsReader(resource);
			m_dataSource = new SingleConnectionDataSource();
			m_dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
		System.out.println("driver OK!! ");
			m_dataSource.setUrl("jdbc:hsqldb:svuni000.replynet.prv:1522:AHZ7O01S");
			m_dataSource.setUsername("AHZ7OP1000");
			m_dataSource.setPassword("AHZ7OP1000");
		System.out.println("data source created");
			m_sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
		System.out.println("sql map created");
			m_dao.setDataSource(m_dataSource);
		System.out.println("Setted data source on DAO");
			m_dao.setSqlMapClient(m_sqlMapClient);
		System.out.println("Setted sql map on DAO");	
		} catch (CannotGetJdbcConnectionException e) {
			System.out.println("no jdbc connection");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("IO Exception");
			e.printStackTrace();
		}
	}


	public void tearDown() {
		
	}
	
	public void testProva(){
		
		bean = new SearchBean();
		bean.setFonte("ACDEK100");
		Integer dimStat = service.findCountStatisticGroup(bean);
		assertEquals(584, dimStat.intValue());
	}
	
}

***********************************************************************

The test-sql-map-config.xml file is:

************************************************************************

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config
2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<!--
    List of all iBatis SQL maps grouped by package name.
-->
<sqlMapConfig>

    <settings useStatementNamespaces="true" />
        
    <sqlMap resource="com/fiat/mida/model/dashboard/dao/SearchRefuse.xml" />
    
</sqlMapConfig>

************************************************************************

And the SearchRefuseDao class is:

**************************************************************************

package com.fiat.mida.model.dashboard.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.fiat.mida.model.dashboard.SearchBean;

public class SearchRefuseDao extends SqlMapClientDaoSupport {

	/** Map for a single thread to hold query parameters. */
	private static ThreadLocal messageKeyOfThread = new ThreadLocal() {
		protected Object initialValue() {
			return new HashMap(3);
		}
	};

	/**
	 * conta il numero di statistiche degli scarti che soddisfano i criteri di
	 * ricerca. Serve per la paginazione
	 * 
	 * @param bean:
	 *            parametri di ricerca
	 * @return: il numero di record di statistiche
	 */
	public Integer getCountStatistics(SearchBean bean) {
		Map messageKey = (Map) messageKeyOfThread.get();
		messageKey.put("sourceCode", bean.getFonte());
		messageKey.put("dataAcq", bean.getDati());
		return (Integer) getSqlMapClientTemplate().queryForObject(
				"SearchRefuse.getCountStatisticGroup", messageKey);

	}

}

************************************************************

When I run this TestCase I have this output:

Apr 20, 2007 1:27:41 PM
org.springframework.jdbc.datasource.DriverManagerDataSource
setDriverClassName
INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver
driver OK!! 
data source created
sql map created

And I don't know why my test stop at that point.

Someone could help me ? 

Thanks a lot for any suggestion,

Have a good day,
Francesco


-- 
View this message in context: http://www.nabble.com/JUnit-Test-with-IBatis-tf3615809.html#a10098759
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: JUnit Test with IBatis

Posted by francesco77 <f....@reply.it>.
Hi to All,

I solved the problem.

There is a problem of DMS which doesn't solve the string
"svuni000.replynet.prv". When I put the IP Address 10.50.1.114 it WORKS
FINE.

Thanks to all

Francesco


francesco77 wrote:
> 
> Dear Marc,
> 
> ops....what a stupid error. Thanks a lot.
> Now the creation of the DAO is correct but I have another error which is:
> 
> org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get
> JDBC Connection; nested exception is java.sql.SQLException: File
> input/output error: java.io.IOException: The filename, directory name, or
> volume label syntax is incorrect
> Caused by: java.sql.SQLException: File input/output error:
> java.io.IOException: The filename, directory name, or volume label syntax
> is incorrect
> 	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
> 	at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
> 	at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
> 	at org.hsqldb.jdbcDriver.connect(Unknown Source)
> 	at java.sql.DriverManager.getConnection(DriverManager.java:512)
> 	at java.sql.DriverManager.getConnection(DriverManager.java:140)
> 	at
> org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291)
> 	at
> org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277)
> 	at
> org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259)
> 	at
> org.springframework.jdbc.datasource.SingleConnectionDataSource.initConnection(SingleConnectionDataSource.java:243)
> 	at
> org.springframework.jdbc.datasource.SingleConnectionDataSource.getConnection(SingleConnectionDataSource.java:186)
> 	at
> org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
> 	at
> org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
> 	at
> org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:182)
> 	at
> org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:241)
> 	at
> com.fiat.mida.model.dashboard.dao.SearchRefuseDao.getCountStatistics(SearchRefuseDao.java:139)
> 	at
> com.fiat.mida.controller.dashboard.AbstractTest.testProva(AbstractTest.java:66)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
> 	at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
> 	at java.lang.reflect.Method.invoke(Method.java:324)
> 	at junit.framework.TestCase.runTest(TestCase.java:154)
> 	at junit.framework.TestCase.runBare(TestCase.java:127)
> 	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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
> 	at
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
> 	at
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
> 
> I don't know if this error is caused by an incorrect
> test-sql-map-config.xml or if I forget to set this configuration file
> somewhere.
> 
> Have you some idea about this error ? 
> 
> Thanks a lot for the help
> 
> Francesco
> 
> 
> 
> marc.heimann wrote:
>> 
>> Wild guess here, but m_dao is never initialized so I assume a 
>> NullPointerException when you try to set the m_dao-Datasource.
>> 
>> Cheers
>> Marc Heimann
>> Software Engineer
>> 
>> Prolifics Deutschland GmbH
>> Notkestr. 3, D-22607 Hamburg
>> phone +49 (0)40 890 667-70
>> fax    +49 (0)40 890 667-99
>> marc.heimann@prolifics.de
>> SOA... Building the Future into Your Business
>> Handelsregister: Hamburg, HRB 89903
>> Geschäftsführer: Ulrich Frotscher
>> 
>> 
>> francesco77 <f....@reply.it> wrote on 20.04.2007 13:30:22:
>> 
>>> 
>>> Hi to All,
>>> 
>>> I'd like to write a TestCase using JUnit to test my DAO.
>>> This DAO works correctly using the WEB Application, but I'd like write a
>>> "standalone" test case for the DAO.
>>> 
>>> So I write this java class :
>>> 
>>> *****************************************************************
>>> 
>>> package com.fiat.mida.controller.dashboard;
>>> 
>>> import java.io.IOException;
>>> import java.io.Reader;
>>> import java.util.Map;
>>> 
>>> import org.springframework.jdbc.CannotGetJdbcConnectionException;
>>> import org.springframework.jdbc.datasource.DriverManagerDataSource;
>>> import org.springframework.jdbc.datasource.SingleConnectionDataSource;
>>> 
>>> import com.fiat.mida.model.dashboard.SearchBean;
>>> 
>>> import junit.framework.TestCase;
>>> 
>>> import com.fiat.mida.model.dashboard.dao.SearchRefuseDao;
>>> import com.fiat.mida.model.dashboard.logic.*;
>>> import com.ibatis.common.resources.Resources;
>>> import com.ibatis.sqlmap.client.SqlMapClient;
>>> import com.ibatis.sqlmap.client.SqlMapClientBuilder;
>>> 
>>> public class AbstractTest extends TestCase{
>>> 
>>>    private SearchRefuseService service;
>>>    private SearchBean bean;
>>>    private String resource =
>>> "com/fiat/mida/controller/dashboard/test-sql-map-config.xml";
>>> 
>>>    private Reader reader ;
>>> 
>>>                 protected DriverManagerDataSource m_dataSource; 
>>>    protected SqlMapClient m_sqlMapClient;
>>>    private SearchRefuseDao m_dao;
>>> 
>>>    protected void setUp() {
>>> 
>>>       try {
>>>          reader = Resources.getResourceAsReader(resource);
>>>          m_dataSource = new SingleConnectionDataSource();
>>>          m_dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
>>>       System.out.println("driver OK!! ");
>>>          m_dataSource.setUrl("jdbc:hsqldb:svuni000.replynet.prv:
>>> 1522:AHZ7O01S");
>>>          m_dataSource.setUsername("AHZ7OP1000");
>>>          m_dataSource.setPassword("AHZ7OP1000");
>>>       System.out.println("data source created");
>>>          m_sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
>>>       System.out.println("sql map created");
>>>          m_dao.setDataSource(m_dataSource);
>>>       System.out.println("Setted data source on DAO");
>>>          m_dao.setSqlMapClient(m_sqlMapClient);
>>>       System.out.println("Setted sql map on DAO"); 
>>>       } catch (CannotGetJdbcConnectionException e) {
>>>          System.out.println("no jdbc connection");
>>>          e.printStackTrace();
>>>       } catch (IOException e) {
>>>          System.out.println("IO Exception");
>>>          e.printStackTrace();
>>>       }
>>>    }
>>> 
>>> 
>>>    public void tearDown() {
>>> 
>>>    }
>>> 
>>>    public void testProva(){
>>> 
>>>       bean = new SearchBean();
>>>       bean.setFonte("ACDEK100");
>>>       Integer dimStat = service.findCountStatisticGroup(bean);
>>>       assertEquals(584, dimStat.intValue());
>>>    }
>>> 
>>> }
>>> 
>>> ***********************************************************************
>>> 
>>> The test-sql-map-config.xml file is:
>>> 
>>> ************************************************************************
>>> 
>>> <?xml version="1.0" encoding="UTF-8" ?>
>>> 
>>> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config
>>> 2.0//EN"
>>>     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
>>> 
>>> <!--
>>     List of all iBatis SQL maps grouped by package name.
>> -->
>>> <sqlMapConfig>
>>> 
>>>     <settings useStatementNamespaces="true" />
>>> 
>>>     <sqlMap 
>> resource="com/fiat/mida/model/dashboard/dao/SearchRefuse.xml" />
>>> 
>>> </sqlMapConfig>
>>> 
>>> ************************************************************************
>>> 
>>> And the SearchRefuseDao class is:
>>> 
>>> 
>> **************************************************************************
>>> 
>>> package com.fiat.mida.model.dashboard.dao;
>>> 
>>> import java.util.HashMap;
>>> import java.util.List;
>>> import java.util.Map;
>>> 
>>> import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
>>> 
>>> import com.fiat.mida.model.dashboard.SearchBean;
>>> 
>>> public class SearchRefuseDao extends SqlMapClientDaoSupport {
>>> 
>>>    /** Map for a single thread to hold query parameters. */
>>>    private static ThreadLocal messageKeyOfThread = new ThreadLocal() {
>>>       protected Object initialValue() {
>>>          return new HashMap(3);
>>>       }
>>>    };
>>> 
>>>    /**
>>>     * conta il numero di statistiche degli scarti che soddisfano i 
>> criteri di
>>>     * ricerca. Serve per la paginazione
>>>     * 
>>>     * @param bean:
>>>     *            parametri di ricerca
>>>     * @return: il numero di record di statistiche
>>>     */
>>>    public Integer getCountStatistics(SearchBean bean) {
>>>       Map messageKey = (Map) messageKeyOfThread.get();
>>>       messageKey.put("sourceCode", bean.getFonte());
>>>       messageKey.put("dataAcq", bean.getDati());
>>>       return (Integer) getSqlMapClientTemplate().queryForObject(
>>>             "SearchRefuse.getCountStatisticGroup", messageKey);
>>> 
>>>    }
>>> 
>>> }
>>> 
>>> ************************************************************
>>> 
>>> When I run this TestCase I have this output:
>>> 
>>> Apr 20, 2007 1:27:41 PM
>>> org.springframework.jdbc.datasource.DriverManagerDataSource
>>> setDriverClassName
>>> INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver
>>> driver OK!! 
>>> data source created
>>> sql map created
>>> 
>>> And I don't know why my test stop at that point.
>>> 
>>> Someone could help me ? 
>>> 
>>> Thanks a lot for any suggestion,
>>> 
>>> Have a good day,
>>> Francesco
>>> 
>>> 
>>> -- 
>>> View this message in context: http://www.nabble.com/JUnit-Test-with-
>>> IBatis-tf3615809.html#a10098759
>>> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>>> 
>> 
>>  
>> 
> 
> 

-- 
View this message in context: http://www.nabble.com/JUnit-Test-with-IBatis-tf3615809.html#a10102527
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: JUnit Test with IBatis

Posted by francesco77 <f....@reply.it>.
Dear Marc,

ops....what a stupid error. Thanks a lot.
Now the creation of the DAO is correct but I have another error which is:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get
JDBC Connection; nested exception is java.sql.SQLException: File
input/output error: java.io.IOException: The filename, directory name, or
volume label syntax is incorrect
Caused by: java.sql.SQLException: File input/output error:
java.io.IOException: The filename, directory name, or volume label syntax is
incorrect
	at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
	at org.hsqldb.jdbc.jdbcConnection.<init>(Unknown Source)
	at org.hsqldb.jdbcDriver.getConnection(Unknown Source)
	at org.hsqldb.jdbcDriver.connect(Unknown Source)
	at java.sql.DriverManager.getConnection(DriverManager.java:512)
	at java.sql.DriverManager.getConnection(DriverManager.java:140)
	at
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291)
	at
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277)
	at
org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259)
	at
org.springframework.jdbc.datasource.SingleConnectionDataSource.initConnection(SingleConnectionDataSource.java:243)
	at
org.springframework.jdbc.datasource.SingleConnectionDataSource.getConnection(SingleConnectionDataSource.java:186)
	at
org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
	at
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
	at
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:182)
	at
org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:241)
	at
com.fiat.mida.model.dashboard.dao.SearchRefuseDao.getCountStatistics(SearchRefuseDao.java:139)
	at
com.fiat.mida.controller.dashboard.AbstractTest.testProva(AbstractTest.java:66)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:324)
	at junit.framework.TestCase.runTest(TestCase.java:154)
	at junit.framework.TestCase.runBare(TestCase.java:127)
	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.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
	at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
	at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

I don't know if this error is caused by an incorrect test-sql-map-config.xml
or if I forget to set this configuration file somewhere.

Have you some idea about this error ? 

Thanks a lot for the help

Francesco



marc.heimann wrote:
> 
> Wild guess here, but m_dao is never initialized so I assume a 
> NullPointerException when you try to set the m_dao-Datasource.
> 
> Cheers
> Marc Heimann
> Software Engineer
> 
> Prolifics Deutschland GmbH
> Notkestr. 3, D-22607 Hamburg
> phone +49 (0)40 890 667-70
> fax    +49 (0)40 890 667-99
> marc.heimann@prolifics.de
> SOA... Building the Future into Your Business
> Handelsregister: Hamburg, HRB 89903
> Geschäftsführer: Ulrich Frotscher
> 
> 
> francesco77 <f....@reply.it> wrote on 20.04.2007 13:30:22:
> 
>> 
>> Hi to All,
>> 
>> I'd like to write a TestCase using JUnit to test my DAO.
>> This DAO works correctly using the WEB Application, but I'd like write a
>> "standalone" test case for the DAO.
>> 
>> So I write this java class :
>> 
>> *****************************************************************
>> 
>> package com.fiat.mida.controller.dashboard;
>> 
>> import java.io.IOException;
>> import java.io.Reader;
>> import java.util.Map;
>> 
>> import org.springframework.jdbc.CannotGetJdbcConnectionException;
>> import org.springframework.jdbc.datasource.DriverManagerDataSource;
>> import org.springframework.jdbc.datasource.SingleConnectionDataSource;
>> 
>> import com.fiat.mida.model.dashboard.SearchBean;
>> 
>> import junit.framework.TestCase;
>> 
>> import com.fiat.mida.model.dashboard.dao.SearchRefuseDao;
>> import com.fiat.mida.model.dashboard.logic.*;
>> import com.ibatis.common.resources.Resources;
>> import com.ibatis.sqlmap.client.SqlMapClient;
>> import com.ibatis.sqlmap.client.SqlMapClientBuilder;
>> 
>> public class AbstractTest extends TestCase{
>> 
>>    private SearchRefuseService service;
>>    private SearchBean bean;
>>    private String resource =
>> "com/fiat/mida/controller/dashboard/test-sql-map-config.xml";
>> 
>>    private Reader reader ;
>> 
>>                 protected DriverManagerDataSource m_dataSource; 
>>    protected SqlMapClient m_sqlMapClient;
>>    private SearchRefuseDao m_dao;
>> 
>>    protected void setUp() {
>> 
>>       try {
>>          reader = Resources.getResourceAsReader(resource);
>>          m_dataSource = new SingleConnectionDataSource();
>>          m_dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
>>       System.out.println("driver OK!! ");
>>          m_dataSource.setUrl("jdbc:hsqldb:svuni000.replynet.prv:
>> 1522:AHZ7O01S");
>>          m_dataSource.setUsername("AHZ7OP1000");
>>          m_dataSource.setPassword("AHZ7OP1000");
>>       System.out.println("data source created");
>>          m_sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
>>       System.out.println("sql map created");
>>          m_dao.setDataSource(m_dataSource);
>>       System.out.println("Setted data source on DAO");
>>          m_dao.setSqlMapClient(m_sqlMapClient);
>>       System.out.println("Setted sql map on DAO"); 
>>       } catch (CannotGetJdbcConnectionException e) {
>>          System.out.println("no jdbc connection");
>>          e.printStackTrace();
>>       } catch (IOException e) {
>>          System.out.println("IO Exception");
>>          e.printStackTrace();
>>       }
>>    }
>> 
>> 
>>    public void tearDown() {
>> 
>>    }
>> 
>>    public void testProva(){
>> 
>>       bean = new SearchBean();
>>       bean.setFonte("ACDEK100");
>>       Integer dimStat = service.findCountStatisticGroup(bean);
>>       assertEquals(584, dimStat.intValue());
>>    }
>> 
>> }
>> 
>> ***********************************************************************
>> 
>> The test-sql-map-config.xml file is:
>> 
>> ************************************************************************
>> 
>> <?xml version="1.0" encoding="UTF-8" ?>
>> 
>> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config
>> 2.0//EN"
>>     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
>> 
>> <!--
>     List of all iBatis SQL maps grouped by package name.
> -->
>> <sqlMapConfig>
>> 
>>     <settings useStatementNamespaces="true" />
>> 
>>     <sqlMap 
> resource="com/fiat/mida/model/dashboard/dao/SearchRefuse.xml" />
>> 
>> </sqlMapConfig>
>> 
>> ************************************************************************
>> 
>> And the SearchRefuseDao class is:
>> 
>> 
> **************************************************************************
>> 
>> package com.fiat.mida.model.dashboard.dao;
>> 
>> import java.util.HashMap;
>> import java.util.List;
>> import java.util.Map;
>> 
>> import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
>> 
>> import com.fiat.mida.model.dashboard.SearchBean;
>> 
>> public class SearchRefuseDao extends SqlMapClientDaoSupport {
>> 
>>    /** Map for a single thread to hold query parameters. */
>>    private static ThreadLocal messageKeyOfThread = new ThreadLocal() {
>>       protected Object initialValue() {
>>          return new HashMap(3);
>>       }
>>    };
>> 
>>    /**
>>     * conta il numero di statistiche degli scarti che soddisfano i 
> criteri di
>>     * ricerca. Serve per la paginazione
>>     * 
>>     * @param bean:
>>     *            parametri di ricerca
>>     * @return: il numero di record di statistiche
>>     */
>>    public Integer getCountStatistics(SearchBean bean) {
>>       Map messageKey = (Map) messageKeyOfThread.get();
>>       messageKey.put("sourceCode", bean.getFonte());
>>       messageKey.put("dataAcq", bean.getDati());
>>       return (Integer) getSqlMapClientTemplate().queryForObject(
>>             "SearchRefuse.getCountStatisticGroup", messageKey);
>> 
>>    }
>> 
>> }
>> 
>> ************************************************************
>> 
>> When I run this TestCase I have this output:
>> 
>> Apr 20, 2007 1:27:41 PM
>> org.springframework.jdbc.datasource.DriverManagerDataSource
>> setDriverClassName
>> INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver
>> driver OK!! 
>> data source created
>> sql map created
>> 
>> And I don't know why my test stop at that point.
>> 
>> Someone could help me ? 
>> 
>> Thanks a lot for any suggestion,
>> 
>> Have a good day,
>> Francesco
>> 
>> 
>> -- 
>> View this message in context: http://www.nabble.com/JUnit-Test-with-
>> IBatis-tf3615809.html#a10098759
>> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>> 
> 
>  
> 

-- 
View this message in context: http://www.nabble.com/JUnit-Test-with-IBatis-tf3615809.html#a10101307
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.


Re: JUnit Test with IBatis

Posted by Ma...@prolifics.de.
Wild guess here, but m_dao is never initialized so I assume a 
NullPointerException when you try to set the m_dao-Datasource.

Cheers
Marc Heimann
Software Engineer

Prolifics Deutschland GmbH
Notkestr. 3, D-22607 Hamburg
phone +49 (0)40 890 667-70
fax    +49 (0)40 890 667-99
marc.heimann@prolifics.de
SOA... Building the Future into Your Business
Handelsregister: Hamburg, HRB 89903
Geschäftsführer: Ulrich Frotscher


francesco77 <f....@reply.it> wrote on 20.04.2007 13:30:22:

> 
> Hi to All,
> 
> I'd like to write a TestCase using JUnit to test my DAO.
> This DAO works correctly using the WEB Application, but I'd like write a
> "standalone" test case for the DAO.
> 
> So I write this java class :
> 
> *****************************************************************
> 
> package com.fiat.mida.controller.dashboard;
> 
> import java.io.IOException;
> import java.io.Reader;
> import java.util.Map;
> 
> import org.springframework.jdbc.CannotGetJdbcConnectionException;
> import org.springframework.jdbc.datasource.DriverManagerDataSource;
> import org.springframework.jdbc.datasource.SingleConnectionDataSource;
> 
> import com.fiat.mida.model.dashboard.SearchBean;
> 
> import junit.framework.TestCase;
> 
> import com.fiat.mida.model.dashboard.dao.SearchRefuseDao;
> import com.fiat.mida.model.dashboard.logic.*;
> import com.ibatis.common.resources.Resources;
> import com.ibatis.sqlmap.client.SqlMapClient;
> import com.ibatis.sqlmap.client.SqlMapClientBuilder;
> 
> public class AbstractTest extends TestCase{
> 
>    private SearchRefuseService service;
>    private SearchBean bean;
>    private String resource =
> "com/fiat/mida/controller/dashboard/test-sql-map-config.xml";
> 
>    private Reader reader ;
> 
>                 protected DriverManagerDataSource m_dataSource; 
>    protected SqlMapClient m_sqlMapClient;
>    private SearchRefuseDao m_dao;
> 
>    protected void setUp() {
> 
>       try {
>          reader = Resources.getResourceAsReader(resource);
>          m_dataSource = new SingleConnectionDataSource();
>          m_dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
>       System.out.println("driver OK!! ");
>          m_dataSource.setUrl("jdbc:hsqldb:svuni000.replynet.prv:
> 1522:AHZ7O01S");
>          m_dataSource.setUsername("AHZ7OP1000");
>          m_dataSource.setPassword("AHZ7OP1000");
>       System.out.println("data source created");
>          m_sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
>       System.out.println("sql map created");
>          m_dao.setDataSource(m_dataSource);
>       System.out.println("Setted data source on DAO");
>          m_dao.setSqlMapClient(m_sqlMapClient);
>       System.out.println("Setted sql map on DAO"); 
>       } catch (CannotGetJdbcConnectionException e) {
>          System.out.println("no jdbc connection");
>          e.printStackTrace();
>       } catch (IOException e) {
>          System.out.println("IO Exception");
>          e.printStackTrace();
>       }
>    }
> 
> 
>    public void tearDown() {
> 
>    }
> 
>    public void testProva(){
> 
>       bean = new SearchBean();
>       bean.setFonte("ACDEK100");
>       Integer dimStat = service.findCountStatisticGroup(bean);
>       assertEquals(584, dimStat.intValue());
>    }
> 
> }
> 
> ***********************************************************************
> 
> The test-sql-map-config.xml file is:
> 
> ************************************************************************
> 
> <?xml version="1.0" encoding="UTF-8" ?>
> 
> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config
> 2.0//EN"
>     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
> 
> <!--
    List of all iBatis SQL maps grouped by package name.
-->
> <sqlMapConfig>
> 
>     <settings useStatementNamespaces="true" />
> 
>     <sqlMap 
resource="com/fiat/mida/model/dashboard/dao/SearchRefuse.xml" />
> 
> </sqlMapConfig>
> 
> ************************************************************************
> 
> And the SearchRefuseDao class is:
> 
> 
**************************************************************************
> 
> package com.fiat.mida.model.dashboard.dao;
> 
> import java.util.HashMap;
> import java.util.List;
> import java.util.Map;
> 
> import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
> 
> import com.fiat.mida.model.dashboard.SearchBean;
> 
> public class SearchRefuseDao extends SqlMapClientDaoSupport {
> 
>    /** Map for a single thread to hold query parameters. */
>    private static ThreadLocal messageKeyOfThread = new ThreadLocal() {
>       protected Object initialValue() {
>          return new HashMap(3);
>       }
>    };
> 
>    /**
>     * conta il numero di statistiche degli scarti che soddisfano i 
criteri di
>     * ricerca. Serve per la paginazione
>     * 
>     * @param bean:
>     *            parametri di ricerca
>     * @return: il numero di record di statistiche
>     */
>    public Integer getCountStatistics(SearchBean bean) {
>       Map messageKey = (Map) messageKeyOfThread.get();
>       messageKey.put("sourceCode", bean.getFonte());
>       messageKey.put("dataAcq", bean.getDati());
>       return (Integer) getSqlMapClientTemplate().queryForObject(
>             "SearchRefuse.getCountStatisticGroup", messageKey);
> 
>    }
> 
> }
> 
> ************************************************************
> 
> When I run this TestCase I have this output:
> 
> Apr 20, 2007 1:27:41 PM
> org.springframework.jdbc.datasource.DriverManagerDataSource
> setDriverClassName
> INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver
> driver OK!! 
> data source created
> sql map created
> 
> And I don't know why my test stop at that point.
> 
> Someone could help me ? 
> 
> Thanks a lot for any suggestion,
> 
> Have a good day,
> Francesco
> 
> 
> -- 
> View this message in context: http://www.nabble.com/JUnit-Test-with-
> IBatis-tf3615809.html#a10098759
> Sent from the iBATIS - User - Java mailing list archive at Nabble.com.
>