Monty
Monty

Reputation: 37

Spring beans value injection

I'm starting work with the Spring framework and I'm trying to wrap my head around the beans concept. I have an xml file:

<?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>

And a class to get the current date:

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

    }

What I'm trying to accomplish is a simple @test to assert if the bean value is the same as the current date i supply.

What I'm stuck at is:

@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);
}

I'm unaware of how i can supply the value of the bean in to the test, and i was wondering how to accomplish it and if there was any good tutorials/documentations except for the spring docs themselves

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);
    }

}

Why is this not printing the formatted date?

Upvotes: 0

Views: 616

Answers (1)

Beri
Beri

Reputation: 11600

Use spring support for testing. Code will be cleaner and easyer to understand.

@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);
}
}

I think you wanted to do something like this:

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>

Upvotes: 1

Related Questions