Я начинаю работать с фреймворком Spring, и я пытаюсь обернуть голову вокруг концепции бобов. У меня есть файл 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 утверждать, если значение боб является так же, как и текущая дата, которую я поставлю.
То, что я застрял на это:
@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);
}
Я не знаю о том, как я могу поставить значение компоненты в испытанию, и мне было интересно, как осуществить это, и если есть любая хорошие учебники/документация для пружинных самих документов, кроме
Edit:
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);
}
}
Почему это не печатает отформатированную дату?
Просто позвоните 'currentDateServiceObj.getCurrentDate()' – Jens
Там нет необходимости делать все вручную. Для нового кода я обычно рекомендую JavaConfig над XML и, конечно, встраивание в bean lookup. – chrylis