Spring unit test

xiaoxiao2021-03-06  40

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);}}

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

New Post(0)