How to check if-statement in method using Mockito and JUnit?

前端 未结 3 778
时光说笑
时光说笑 2021-01-15 09:05

I have method that I should test. Code (of course some parts were cut):

public class FilterDataController {

    public static final String DATE_FORMAT = \"y         


        
3条回答
  •  长情又很酷
    2021-01-15 09:46

    I see two main approaches.

    1. Using Mockito functionality: if you inject your controller with a mock FilterDataProvider (which is the standard approach, using for example MockitoJUnitRunner and @InjectMocks) , then you can use Mockito's "verity" option to make sure it got the correct endDate. See discussion: http://www.vogella.com/tutorials/Mockito/article.html#mockito_verify
    2. Obviously, there are other approaches that rely on logic rather than technicalities. For example: refactoring the "if" part to a separateMethod "correctEndDate", or populating your data so that a different list of coutries is returned based on the endDate.

提交回复
热议问题