我开始使用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);
}
}
为什么不打印格式化日期?
答案 0 :(得分: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>