Spring unit test

xiaoxiao2021-03-06  37

1: I have a bean configured in Web-INF /Web-inf/persist/jdbc.properties /Web-inf/persist/sqlmapconfig.xml If the unit test uses FileSystemXmlapplicationContext (LOCS) to get ApplicationContext, the path in the above configuration segment Can't find it. 2:

Alin_ASS wrote: I will try it, do you have a ready-made test code? My structure is such a webapp | | - wean1.xml ..... | - Bean2.xml ..... | - bean3.xml ..... | --- class .................. | --- mytestcase1.class

Move your bean.xml to the ClassPath (Classes directory), such as com.javaeye below this package. Then we can write this way in web.xml:

XML code:

contextconfigLocation classpath: /com/javaeye/bean1.xml, classpath: /com/javaeye/bean2.xml < / context-param>

Unit test ClassPathXMLApplicationContext

Java code:

ClassPathXMLApplicationContext ApplicationContext = New classpathXMLApplicationContext (new string [] {"com / javaeye / bean1.xml", "COM / javaeye / bean2.xml"});

Location of those JDBC Property and Ibatis mapping files can also be written into classpath: xxx.properties is the benefit of such a JAR package to put all the stuff into it ... 3:

I have learned a trick, but according to my directory, the configuration file is relatively comfortable. I have to write trouble to pull it. But I don't know how to control database transactions in the test. For example, the following UseractuPdaOimpteeeCcount () uses SqlmapClient.StartTranscation () committranscation () ... there will be questions through test Java code:

public class DaoImpTest extends TestCase {protected final void initDaoImp ​​(SqlMapClientDaoSupport daoImp) throws Exception {String classesDirLoc = ClassLoader.getSystemResource ( "") getFile ();. File webinfDir = new File (classesDirLoc) .getParentFile (); // init data source InputStream is = new FileInputStream (new File (webinfDir, "jdbc.properties")); Properties jdbcProperties = new Properties (); jdbcProperties.load (is); BasicDataSource basicDS = new BasicDataSource (); basicDS.setDriverClassName (jdbcProperties .getProperty ( "jdbc.driverClassName")); basicDS.setUrl (jdbcProperties.getProperty ( "jdbc.url")); basicDS.setUsername (jdbcProperties.getProperty ( "jdbc.username")); basicDS.setPassword (jdbcProperties.getProperty ( "jdbc .password ")); DataSource ds = basicDS; // init sqlMapClient Reader reader = new FileReader (new File (webinfDir, "sqlMapConfig.xml")); SqlMapClient sqlMapClient = new XmlSqlMapClientBuilder () .buildSqlMap (reader); // init daoImp ​​daoImp.setDataSource (ds ); DAOIMP.SETSQLMAPCLIENT (SQLMAPCLIENT);}}

Java code:

public class UserAccountDaoImpTest extends DaoImpTest {private UserAccountDao dao; protected void setUp () throws Exception {UserAccountDaoImp ​​daoImp ​​= new UserAccountDaoImp ​​(); this.initDaoImp ​​(daoImp); this.dao = daoImp;} public void testGetInsertUpdateDeleteAccount () {...} } JAVA code:

Public Class Useraccountdaimp Extends SqlmapClientDaosupport IMPLEments USERACCOUNTDAO {...}

4: Unit testing of Spring Dao does encounter issues, OpenSessionInview, etc., Spring Forum has long talked about these issues. It is recommended to go directly to his official forum to find, which is more actually. Take some code segmentation base class with DBUnit: SpringTestCase

Java code:

Abstract Public Class SpringtestCase Extends DatabaseTestCase

Inheriting DatabaseTestCase needs to copy several methods getConnection method is DBUnit to get a database connection method, which I have set the database properties as Oracle, because the default DBUnit does not set, the specific document DBUnit writes a very detailed test environment required to test the data required Source, data files, database properties I have used Properties files to external settings, so the props.getProperty statement will appear, which is not necessary. You can remove this statement.

Java code:

protected IDatabaseConnection getConnection () throws Exception {DataSource ds = (DataSource) getBean (props.getProperty (BEAN_DATASOURCE)); IDatabaseConnection conn = new DatabaseConnection (ds.getConnection (), props.getProperty (TEST_SCHEMA)); conn.getConfig (). SetProperty (DatabaseConfig.property_DataType_Factory, New OracleDataTypeFactory ()); Return Conn;}

GetDataSet method Set your test data source, that is, the data file. Here I am set up a data file in XML format.

Java code:

protected IDataSet getDataSet () throws Exception {try {InputStream in = new FileInputStream (props.getProperty (APP_DATASET)); return new XmlDataSet (in);} catch (FileNotFoundException ex) {log.error (ex); throw new SpringTestCaseException (ex Anyone who wants to test all DAO will inherit from the base class. This way you don't have to worry about data pollution. As for the transaction problem in the test, I don't have more Luo, and I posted the code. Everyone knows how to use this class in the test.

Java code:

package com.idealbiz.test; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.orm.hibernate.HibernateTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import Org.springframework.transaction.transactionstatus; import org.springframework.transaction.support.defaultTransactionDefinition; import net.sf.hibernate.SessionFactory;

/ ** * $ ID: HibernateTransactionUtils.java, V 1.9 2004/09/03 17:23:14 DENIS EXP $ * Class HibernateTransActionUtils * *

Shanghai Ideal Information Industry (Group) Co., Ltd. * @author denis deniswang@sina.com * 2004-9-2 17:50:02 * / public class HibernateTransactionUtils {private static final log log = LogFactory .getLog (HibernateTransactionUtils.class); private PlatformTransactionManager ptm; private TransactionStatus status; private static HibernateTransactionUtils transactionUtils; protected HibernateTransactionUtils (SessionFactory sessionFactory) {log.info ( "Initialize HibnernateTransactionManager ..."); ptm = new HibernateTransactionManager (sessionFactory);} public static HibernateTransactionUtils getNewInstance (SessionFactory sessionFactory) {if (TransactionUtils == null) {TransactionUtils = new hibernateTransactionUtils (sessionFactory);} Return TransactionUtils;} PUBLIC VOID Log.info ("Begin Transaction ..."); status = ptm.gettransaction (new defaultTransactionDefinition ()); public void rollbacktransaction () {if (status! = null) {log.info ("Rollback Transaction ... "); Ptm.rollback (status);} else {throw new IllegalStateException (" Transaction Status INCORRECT);}} public void committor () {if (status! = Null) {log.info ("Commit Transaction .. "); Ptm.commit (status);} else {throw new IllegalStateException (" Transaction Status IncorRect);}}}

If you use OpenSessionInView, you may need to imitate this environment to avoid lazy loading abnormalities in the test environment. Test web action. Then the test class only needs to inherit the class below. Java code:

package com.idealbiz.test; import org.springframework.orm.hibernate.SessionFactoryUtils; import org.springframework.orm.hibernate.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; import net.sf.hibernate.Session; import Net.sf.Hibernate.SessionFactory;

转载请注明原文地址:https://www.9cbs.com/read-66157.html

New Post(0)