如何在Java单元测试中模拟/赋值LocalDate变量

huangapple go评论114阅读模式
英文:

How to Mock/Assign LocalDate variable in Unit Test in Java

问题

我有一个LocalDate变量,它是从配置属性中填充的。我需要对一个使用这个变量的方法进行测试。但是它抛出了错误“无法模拟LocalDate”。我查阅了Stack Overflow和其他网站上的各种文章,但无论在哪里,它都在谈论“DateTimeProviders”,但由于我正在维护客户的现有项目,所以不能使用它。现在,问题是我需要知道如何将模拟分配给类内的变量endDate,并从测试中调用。

我的类:

public class MyClass implements MyClassService{
    
    @Value("#{T(java.time.LocalDate).parse('${configuration.entityconfig.end_date}')}")
    private LocalDate endDate;
    
    @Autowired
    public WpLateDepartureObligationServiceImpl(....){
         //一些操作
    }    
    
    public void createApplications(MyEntity myEntity) {
       
        if (myEntity.getExpiryDate.isBefore(endDate)){
             return;
        }
    }
}

这是单元测试区的样子:

@InjectMocks
private MyClass myClassService;

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

我真的不知道如何传递这个值。我尝试了模拟,但不起作用。有没有办法可以从createApplicationTest方法中传递endDate的值呢?

英文:

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?

答案1

得分: 0

我通过使用以下方法解决了这个问题:

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

尽管我无法从配置中导入数据,但这个方法满足了我将值发送到“test”类变量的目的。

英文:

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.

huangapple
  • 本文由 发表于 2020年4月6日 14:56:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/61054452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定