????????????????????У????????????? Spring ????????????????ж??????????????????????????? Spring test ?????????????

?????????????????? Spring ?? XML ???????????? <context:annotation-config/> ?У?????????

?????嵥 6. Spring-db1.xml

    <beans xmlns="http://www.Springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.Springframework.org/schema/beans 
      
   http://www.Springframework.org/schema/beans/Spring-beans-3.2.xsd">
      
     <bean id="datasource"
    >
             <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
             <property name="url" value="jdbc:hsqldb:hsql://localhost" />
             <property name="username" value="sa"/>
             <property name="password" value=""/>
         </bean>
     <bean id="transactionManager"
    >
             <property name="dataSource" ref="datasource"></property>
         </bean>
         <bean id="initer" init-method="init">
         </bean>
     <bean id="accountDao" depends-on="initer">
             <property name="dataSource" ref="datasource"/>
         </bean>
         <bean id="accountService">
         </bean>
     </beans>

???????е? transactionManager ?? Spring test ????????????????????????

?????嵥 7. AccountServiceTest1.Java

    package service; 
    import static org.Junit.Assert.assertEquals; 
      
    import org.Junit.Test; 
    import org.Junit.runner.RunWith; 
    import org.Springframework.beans.factory.annotation.Autowired; 
    import org.Springframework.test.context.ContextConfiguration; 
    import org.Springframework.test.context.Junit4.SpringJUnit4ClassRunner; 
    import org.Springframework.transaction.annotation.Transactional; 
      
    import domain.Account; 
      
    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration("/config/Spring-db1.xml") 
    @Transactional
    public class AccountServiceTest1 { 
        @Autowired
        private AccountService service; 
      
        @Test
        public void testGetAcccountById() { 
    Account acct = Account.getAccount(1?? "user01"?? 18?? "M"); 
            service.insertIfNotExist(acct); 
            Account acct2 = service.getAccountById(1); 
            assertEquals(acct??acct2); 
        } 
    }

????????????????£?

?????? @RunWith ??????? Junit ??????????????????????????????????? SpringJUnit4ClassRunner??????????????? Junit ???л?????????????????????????? Spring ?????? Junit4.5 ??????????????????????????????

?????? @ContextConfiguration ??????? Spring test context ???????????? Spring ???????????????????? XML ???λ????? Spring ????????????????????? classpath ?μ? /config/Spring-db1.xml ??????????λ??

?????? @Transactional ??????????????????????????????????е???????????????? rollback??????????????????????????κζ???????????

?????? @Autowired ????????????????????? Spring ???????й??????????????????? bean ?????????????????????????? bean ?????

?????? testGetAccountById ????????????????????????? AccountServiceOldTest ??????????????????????????????? try-catch-finally ????????????????У???????????????????????????????

??????н??

?????? Eclipse ?? Junit ????У??????????????μ?????

????? 3. ??н??

????С??

?????????????? Spring ?????н??е?????????????????????????

?????? ??????? Junit4 ????????????? @Test ?????????????????????壬??Щ???????

?????? ???????? @RunWith(SpringJUnit4ClassRunner.class) ????? Spring ???????????

?????? ???????? @ContextConfiguration ?????????? Spring ??????????????????λ??

?????? ???????? @Transactional ??????????????????

?????? ????????? @Autowired ?????? Spring ?? bean ????????

???????????????????

?????? ??????? Spring ?????????

?????? ???????????????α??

?????? ?????? application context ????? bean ???