wonderer's program

          everything will be better
          posts - 19, comments - 6, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          JUnit+Spring入門

          Posted on 2007-07-18 11:12 wonderer 閱讀(1362) 評論(0)  編輯  收藏 所屬分類: java

          首先要導入包

          1:Spring支持包:spring.jar , commons-logging.jar

          2: JUnit支持包: JUnit.jar

          image

          建立Bean類,

             1: package refBeanDemo;
             2:  
             3: import java.util.Date;
             4:  
             5: public class HelloBean {
             6:     private String helloWorld;
             7:     private Date date;
             8:     public Date getDate() {
             9:         return date;
            10:     }
            11:     public void setDate(Date date) {
            12:         this.date = date;
            13:     }
            14:     public String getHelloWorld() {
            15:         return helloWorld;
            16:     }
            17:     public void setHelloWorld(String helloWorld) {
            18:         this.helloWorld = helloWorld;
            19:     }
            20:     
            21: }

           

          建立配置文件,和在里面進行注入

             1: <?xml version="1.0" encoding="UTF-8"?>
             2: <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "../resources/spring-beans-2.0.dtd" >
             3: <beans>
             4:     <bean id="dateBean" class="java.util.Date"></bean>
             5:     
             6:     <bean id="helloBean" class="refBeanDemo.HelloBean">
             7:         <property name="helloWorld">
             8:             <value>你好,世界</value>
             9:         </property>
            10:         <property name="date" ref="dateBean"></property>
            11:     </bean>
            12: </beans>

          寫JUnit進行測試,方便管理,把JUnit的東東放到test包里。

             1: package refBeanDemo;
             2:  
             3: import org.springframework.context.ApplicationContext;
             4: import org.springframework.context.support.ClassPathXmlApplicationContext;
             5:  
             6: import junit.framework.TestCase;
             7:  
             8: public class TestRefBeanDemo extends TestCase {
             9:     private ApplicationContext context;
            10:  
            11:     public void setUp() {
            12:         context = new ClassPathXmlApplicationContext("refBeanDemo/NewFile.xml");
            13:     }
            14:  
            15:     public void testSpring() {
            16:         HelloBean helloBean = (HelloBean)context.getBean("helloBean");
            17:         System.out.println(helloBean.getDate());
            18:         assertEquals("你好,世界", helloBean.getHelloWorld());
            19:         
            20:     }
            21: }

           

          運行JUnit測試

          image

          測試成功。類的分布如下:

          image

          image

          主站蜘蛛池模板: 佳木斯市| 深水埗区| 遂溪县| 屏边| 右玉县| 杂多县| 汝州市| 南岸区| 固阳县| 汨罗市| 喀什市| 固镇县| 楚雄市| 台北县| 博野县| 水富县| 甘谷县| 庄河市| 防城港市| 酒泉市| 读书| 嘉荫县| 孟津县| 漳州市| 连江县| 瑞金市| 额尔古纳市| 宁明县| 甘南县| 嘉兴市| 卢湾区| 武功县| 商河县| 台中市| 临湘市| 高陵县| 永年县| 苏尼特右旗| 文化| 手机| 洪洞县|