java 單元測試

來源:互聯網
上載者:User

標籤:text   xml檔案   model   nfa   get   ann   載入   driver   list   

在測試檔案夾下建立測試基礎類,內容為

//指定bean注入的設定檔@ContextConfiguration(locations = { "classpath:application.xml" })//使用標準的JUnit @RunWith注釋來告訴JUnit使用Spring TestRunner@RunWith(SpringJUnit4ClassRunner.class)public abstract class SpringTestCase extends AbstractJUnit4SpringContextTests{    protected Logger logger = LoggerFactory.getLogger(getClass());}

此測試類別用於spring配置的項目,需載入xml檔案時用到,測試程式繼承此類即可測試,比如:

public class UserServiceTest extends SpringTestCase    {    @Resource    private UserService userService;    Logger logger = Logger.getLogger(UserServiceTest.class);        @Test    public void selectUserByIdTest(){        User user = userService.selectUserById(10);        logger.info("尋找結果" + user);    }    }

application.xml例子

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop"    xsi:schemaLocation="             http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd">          <!-- 引入jdbc設定檔 -->       <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>               <value>classpath:properties/*.properties</value>                <!--要是有多個設定檔,只需在這裡繼續添加即可 -->            </list>        </property>    </bean>            <!-- 配置資料來源 -->    <bean id="dataSource"        class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <!-- 不使用properties來配置 -->        <!-- <property name="driverClassName" value="com.mysql.jdbc.Driver" />             <property name="url" value="jdbc:mysql://localhost:3306/learning" />             <property name="username" value="root" />             <property name="password" value="[email protected]" /> -->       <!-- 使用properties來配置 -->        <property name="driverClassName">            <value>${jdbc_driverClassName}</value>        </property>        <property name="url">            <value>${jdbc_url}</value>        </property>        <property name="username">            <value>${jdbc_username}</value>        </property>        <property name="password">            <value>${jdbc_password}</value>        </property>    </bean>    <!-- 自動掃描了所有的XxxxMapper.xml對應的mapper介面檔案,這樣就不用一個一個手動設定Mpper的映射了,只要Mapper介面類和Mapper對應檔對應起來就可以了。 -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">        <property name="basePackage"            value="com.lin.dao" />    </bean>    <!-- 配置Mybatis的檔案 ,mapperLocations配置**Mapper.xml檔案位置,configLocation配置mybatis-config檔案位置-->    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <property name="mapperLocations" value="classpath*:com/lin/mapper/**/*.xml"/>          <property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />        <!-- <property name="typeAliasesPackage" value="com.tiantian.ckeditor.model"             /> -->    </bean>    <!-- 自動掃描註解的bean -->    <context:component-scan base-package="com.lin.service" /></beans>

 

java 單元測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.