-2

I have a LocalDate variable which is populated from configuration properties. I need to do testing on a method which uses this variable. But it throws the error LocalDate cannot be mocked. I went through various articles in Stack Overflow and other web sites, but everywhere it's talking about DateTimeProviders which cant be used because I'm maintaining an existing project for the client. Now, the problem is I need to know how can I assign the Mock to the variable endDate inside the class from the Test

My Class

public class MyClass implements MyClassService{

    @Value("#{T(java.time.LocalDate).parse('${configuration.entityconfig.end_date}')}")
    private LocalDate endDate;

    @Autowired
    public WpLateDepartureObligationServiceImpl(....){
         //some things
    }    

    public void createApplications(MyEntitity myEntity) {

    if (myEntity.getExpiryDate.isBefore(endDate)){
         return;
    }

}

Here is the UnitTest area looks like

  @InjectMocks
     private MyClass myClassService;

     @Test
     public void createApplicationTest() {
        MyEntitity myEntity=new MyEntitity ();
        myEntity.setId(1L);
        myEntity.setExpiryDate(LocalDate.parse("2020-04-05"));    

        myClassService.createApplications(myEntity);
     }

I'm really lost on how to send the value. I tried mocking it, but it does not work. Is there a way I can send the endDate from the method createApplicationTest?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
smilu
  • 859
  • 7
  • 30
  • 53
  • Why not set a test configuration with whatever end date you want? That's the point of externalising the config. – jonrsharpe Apr 06 '20 at 07:03
  • Im too new to this. So, do you mean to add a properties file for test alone? and also how to still pass the endDate? – smilu Apr 06 '20 at 07:06
  • Then read e.g. https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config, which runs through the various options. I don't know what you mean by *"still pass the endDate"*. – jonrsharpe Apr 06 '20 at 07:08
  • No, you understood my problem right. I am calling the method. but enddate cannot be populated bcz its not Mocked from Test. But the test does not allow me to Mock it saying LocalDate is not allowed to Mock. – smilu Apr 06 '20 at 07:10
  • Please give a [mre]. I'm not suggesting mocking it, exactly, I'm suggesting setting new external configuration using the various methods in the link I shared (either a specific test profile or setting specific properties using option 2 or 3). – jonrsharpe Apr 06 '20 at 07:13
  • ok Thank you :) i found a way to pass values using ReflectionTestUtils.setField – smilu Apr 06 '20 at 08:03

1 Answers1

0

I fixed this problem by using

 ReflectionTestUtils.setField(myClassService,endDate,LocalDate.Now());

Even though I was not able to import the data from configuration, this served my purpose to send value to a variable from test class.

smilu
  • 859
  • 7
  • 30
  • 53