2014-10-06 67 views
0

我開始與Spring框架合作,並試圖圍繞bean概念進行包裝。 我有一個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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id= "currentDateService" class ="xx.CurrentDateSerivceimpl" /> 
</beans> 

而一個類來獲得當前日期:

public class CurrentDateServiceImpl implements CurrentDateService { 
    public LocalDate getCurrentDate() { 
     return LocalDate.now() ; 

    } 

我試圖做到的是一個簡單的@Test斷言如果bean值是與我提供的當前日期相同。

什麼我被困在是:

@Test 
public void test() { 
    ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml"); 
    CurrentDateServiceImpl currentDateServiceObj = (CurrentDateServiceImpl) context.getBean("currentDateService"); 
    LocalDate date = LocalDate.now(); 
    LocalDate date2 = "the value of the bean"; 
    assertEquals(date, date2); 
} 

,我不知道我怎麼可以給測試提供bean的價值,我想知道如何完成它,如果有除了春天文檔本身任何好的教程/單證

編輯:

package lt.insoft.app.bl.service.impl; 

import static org.junit.Assert.assertEquals; 

import java.time.LocalDate; 

import lt.insoft.app.bl.service.CurrentDateService; 
import lt.insoft.app.bl.service.CurrentDateServiceFormat; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "file:src/main/resources/META-INF/application-context.xml" }) 
public class CurrentDateServiceImplTest { 

@Autowired 
CurrentDateService service; 
CurrentDateServiceFormat service2; 


    @Test 
    public void test() { 

     LocalDate date = LocalDate.now(); 
     LocalDate date2 = service.getCurrentDate();  
     String date3 = service2.formatCurrentDate(); 
     System.out.println(date3); 
     assertEquals(date, date2); 
    } 

} 

這是爲什麼不打印格式化的日期?

+1

只需調用'currentDateServiceObj.getCurrentDate()' – Jens 2014-10-06 05:24:11

+0

有沒有必要用手工做的一切。對於新代碼,我通常推薦使用JavaConfig over XML,當然也可以注入bean查找。 – chrylis 2014-10-06 06:01:53

回答

1

使用彈簧支持進行測試。代碼將更清晰,更容易理解。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"application-context.xml"}) 
public class TestClass{ 

@Autowired 
CurrentDateService service; 

@Test 
public void test() { 

    LocalDate date = LocalDate.now(); 
    LocalDate date2 = service.getCurrentDate(); 
    assertEquals(date, date2); 
} 
} 

我覺得你想要做這樣的事情:

public class CurrentDateServiceImpl implements CurrentDateService { 
    public LocalDate getCurrentDate() { 
     return LocalDate.now() ; 

    } 
} 

public class CurrentDateServiceFormatImpl implements CurrentDateServiceFormat{ 
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("dd-MM-yyyy"); 

    CurrentDateService service; 

    public void myMethod(){ 
     return service.getCurrentDate().format(FORMATTER); 
    } 

    public void setService(CurrentDateService service){ 
     this.service = service; 
    } 
} 

<?xml version="1.0" encoding="UTF-8"?> 
<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.xsd"> 

    <bean id= "currentDateService" class ="xx.CurrentDateSerivceimpl" /> 
    <bean id= "CurrentDateServiceFormat" class ="xx.CurrentDateServiceFormatImpl"> 
    <property name="service" id-ref="currentDateService"/> 
    </bean> 
</beans> 
+0

謝謝,儘管我有一個後續問題,但似乎我並不理解bean是什麼,應該在這裏進一步發佈問題嗎? – Monty 2014-10-06 05:32:16

+0

好吧,讓我試試exmplain。在普通的Java應用程序(控制檯)中,您必須創建並傳遞許多類實例。在春天你不需要。你告訴spring(在xml定義中)如何創建z java類(例如:它有什麼參數),然後當你創建一個bean的時候,你可以簡單地讓Spring給你類的實例。所以你正在創建一個由Java實例(Bean)組成的容器,你可以使用它們來填充你的類(其他Beans)的需求。 你可以谷歌「依賴注入」瞭解更多關於此主題的信息。 – Beri 2014-10-06 05:42:24

+0

我試圖瞭解是如何豆之間傳輸值,例如我得到的本地日期和IM試圖改變它的格式爲:) 公共字符串CurrentDateServiceFormat({ DateTimeFormatter格式化= DateTimeFormatter.ofPattern( 「DD-MM-YYYY」); String formattedDateTime = dateTime.format(formatter); return formattedDateTime; } 和dateTime應該等於前一個bean的值,這是我通過getCurrentDate方法獲得的當前日期?但我現在怎麼能把它提供給下一個方法 – Monty 2014-10-06 05:49:51